Multiple inheritance in Python; how to do so in Java?
I'm porting some Python code to Java, and I am having trouble dealing with the following problem:
I have some classes which need to have abilities A, B, or C. Class 1 needs ability A, class 2 needs A, B and C, and class 3 needs B and C. Most importantly, I want to easily be able to change what class can have what ability in the future.
I solved this problem pretty easily with multiple inheritan开发者_开发技巧ce in Python. I'm trying to figure out the best way to do it in Java, but I can't come up with as good of a solution. I know multiple inheritance is frowned-upon, so I'd appreciate being taught a better way.
Thanks!
It depends on your actual use case, but have you already considered decorators?
http://en.wikipedia.org/wiki/Decorator_pattern
Multiple-inheritance ain't frowned upon. What is frowned upon is "implementation inheritance" (also known as "code reuse"), because it leads to the unsolvable "diamond problem". And because, well, code-reuse really hasn't much to do with OO.
What you want to do can be solved using multiple inheritance (and, say, delegation if you need to do "code reuse").
interface A {
void move();
}
interface B {
void eat();
}
interface C {
void think();
}
class One implements A { ... }
class Two implements B { ... }
class Three implements B, C { ... }
Any OOA/OOD using multiple inheritance can be trivially translated to Java. The part where you say that you need to change the "ability" all the time is a bit scary: if, say, a Car
can move()
, why would it suddenly need to be able to think()
?
You can use AspectJ's mixin syntax fairly easily to emulate multiple inheritance (and at compile time too). First, declare an interface for the functionality you want to mixin:
public interface A{
String getSomethingForA();
}
then define an annotation which you can use to signify that you want the mixin applied to a given class:
public @interface WithA {}
then add the annotation to the class you want to use:
@WithA
public class MyClass {}
then, to actually add some functionality:
@Aspect
public class MixinA {
public static class AImpl implements A{
public String getSomethingForA() {
return "it worked!";
}
}
@DeclareMixin("@WithA *")
public static A get() {
return new AImpl();
}
}
You'll need to use the aspectj jars and run the aspects as part of your compile process, but this lets you create truly modularized functionality and then forcibly merge it into your classes later. To access your class with the new functionality, do the following:
MyClass obj = new MyClass();
((A)obj).getSomethingForA();
You can apply the same annotation to another class and cast it as well:
@WithA
@WithB //let's pretend we created this with some other functionality
public class AnotherClass {}
AnotherClass anotherObj = new AnotherClass();
((A)anotherObj).getSomethingForA();
((B)anotherObj).andSetSomethingElseForB("something else");
Multiple inheritance is almost always a bad idea, as its effects can usually be achieved through other mechanisms. Based upon your description of the problem, it sounds like you want to
- Use interfaces to define behavior (public interface A) in this scenario, each behavior should probably have its own interface.
If 2 behaviors are tightly coupled (say A & B), define an interface that implements those two atomic interfaces (public interface CombinedAandB extends A, B)
Define an abstract base class that implements the interface to provide default implementations for behaviors
public abstract class BaseAB implements A, B { @Override public void A() { add(0,1); }
}@Override public void B() {add(1,0); } private void add(int a, int b) //it doesn't return. no soup for you. { a + b; //If you know why this is wrong, high five yourself. }
Define a concrete class that extends the abstract base class, implements another interface, and provides its own behavior.
public class IDoABAndC extends BaseAB implements C { //stuff, etc }
You can define the abilities in interfaces and implement them in your classes.
In java you don't have multiple inheritance, instead you can implement multiple interfaces.
So your class 1 will implement interface A and B. Class 2 will implement interface A, B and C. Class 3 will implement B and C.
If what you need is interface inheritance, then as mentioned before, you can always implement multiple interfaces.
If you're looking for implementation inheritance, you're somewhat out of luck. The best solution is probably to use delegation — replace the extra superclasses with fields, and implement methods that just delegate to those fields. It does require writing a lot of repetitive delegation methods, but it's rather unavoidable in Java (without resorting to AspectJ or other bytecode-munging tricks; careful, this way madness lies …).
This is a bit tangential, but you can have python code running in Java via Jython (http://www.jython.org/). This addresses the porting to Java part, not the solving multiple inheritance part (I think you need to determine which is relevant)
精彩评论