Cheat single inheritance in Java?

2019-02-05 01:43发布

I have heard there is a way to cheat single inheritance and implement multiple inheritance in Java. Does anyone know how to implement this(with out using interface)?

Just out of curiosity ;-)

13条回答
我命由我不由天
2楼-- · 2019-02-05 02:37

There was an effort to bring mixins into Java. Check this link out: http://www.disi.unige.it/person/LagorioG/jam/

查看更多
\"骚年 ilove
3楼-- · 2019-02-05 02:39

Use of composition instead of inheritance tends to be the way around this. This actually also helps a lot with testability, so it's good practice in general.

If you just want your type to "behave" like several other types, you can inherit from as many interfaces as you like, though; you can't "borrow" implementation details from these though, obviously.

查看更多
Bombasti
4楼-- · 2019-02-05 02:39

You could probably "simulate" it by managing the set of superclasses explicitly and using reflection to search all the superclasses for the target method. I wouldn't want to do this in production, but it might an interesting toy program. You could probably do a lot of weird stuff by leveraging reflection, creating classes on the fly, and invoking the compiler programatically.

查看更多
欢心
5楼-- · 2019-02-05 02:40

I was thinking about this a little more and realised that while dynamic proxies will work (it's how RMI (used?) to work), if you really want this sort of functionality you would be better off looking at aspect oriented programming (AOP) using something like AspectJ (eclipse.org/aspectj).

This way you can get several different aspects into a class, giving you pseudo mixin inheritance, without the hideously fragile inheritance heirarchies.

As everyone else has pointed out, wanting/needing multiple inheritance generally indicates you aren't approaching the problem from the right perspective. Remember the GoF principle of "prefer composition over inheritance" for a start!

查看更多
Evening l夕情丶
6楼-- · 2019-02-05 02:44

JAVA doesn't support multiple Inheritence.

You can get it to implement multiple interfaces and some see this as a way round the problem. Personally I have yet to use multiple inheritence, so I can't really understand its appeal.

Normally when someone suggests multiple inheritence within c# or JAVA its due to the fact that 'they could' in c++. Im a fan of 'just because you can doens't mean you should'. As c# & JAVA doesn't support it, why try and force it to do something it wasn't designed to do. This is not to say that there are unique cases where it is a valid technique to empoly, just the code can usually be refactored to not need it.

查看更多
不美不萌又怎样
7楼-- · 2019-02-05 02:46

I believe that the fundamental reason that Java doesn't support multiple inheritance is the same as C#; all objects are ultimately derived from Object and it's having multiple paths to the same base class is ambiguous for the compiler. Ambiguous == Bad, so the compiler doesn't allow it.

Instead, you can simulate multiple inheritance through delegation. See this article for an example.

查看更多
登录 后发表回答