java中的多重继承
Java 不允许从多个类继承(仍然允许从多个接口继承。),我知道它与经典的钻石问题非常一致。 但我的问题是,为什么在从多个基类继承时没有歧义(因此不会出现菱形问题)时,为什么 java 不允许像 C++ 那样的多重继承?
Java is not allowing inheritance from multiple classes (still it allows inheritance from multiple interfaces.), I know it is very much inline with classic diamond problem. But my questions is why java is not allowing multiple inheritance like C++ when there is no ambiguity (and hence no chances of diamond problem) while inheriting from multiple base class ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是 Java 的设计决策。 你永远不会得到它,所以不要太担心。 尽管 MI 可能会帮助您创建 Mixins,但这是 MI 唯一能帮到您的好处。
It was a design decision of Java. You'll never get it, so don't worry too much about it. Although MI might help you make Mixins, that's the only good MI will ever do you.
我读到大多数程序员没有以正确的方式使用多重继承。 在多重继承的情况下,“直接从类继承只是为了重用代码”并不是最佳实践。
在大多数情况下,许多程序员不知道何时使用简单继承。 如果您想拥有良好的设计,则必须谨慎使用多重继承,并且只有在您知道自己在做什么的情况下才可以使用多重继承。
我不认为 java 中缺乏多重继承(如 c++ 中)会对您的代码/应用程序设计/问题域映射到类中施加限制。
I have read that most programmers don't use multiple inheritance in a proper way. "Just go ahead and inherit from a class just to reuse code" is not the best practice in case of multiple inheritance.
Many programmers do not know when to use simple inheritance in most cases. Multiple inheritance must be used with caution and only if you know what you are doing if you want to have a good design.
I don't think that the lack of multiple inheritance in java (as in c++) will put restrictions in your code / application design / problem domain mapping into classes.
简单。 引用 Tom Sintes,
Simplicity. To quote Tom Sintes,
如果java支持多重继承那么它可能会影响java的其他功能
考虑用于调用超类构造函数的 super() 方法。如果程序有多个超类(因为多重继承),那么编译器会混淆应该调用哪个超类构造函数并抛出错误
if java support multiple inheritance then it may effect other features of java
consider super() method which is used to call super class constructor.if program has multiple super class(because of multiple inheritance) then compiler will get confused about which super class constructor should be called and throw an error
Java 设计者做出了这样的决定。 可以通过使用接口来模拟多重继承。
Java designers decided that. Multiple inheritance can be simulated by the use of interfaces.
一个简单的答案是 Java 中的所有类都派生自 java.lang.Object IIRC。 因此,您总是会遇到钻石问题...:-D
One simple answer is that all classes in Java derive from java.lang.Object IIRC. So, you would always have a diamond problem... :-D
确实,Java 并不支持实现的多重继承(仅支持类型,即接口)。 这是一个设计决定。
但是,从 Java 8 开始,它支持使用默认方法的多重继承。 请参阅 http://docs.oracle.com/javase/tutorial/java/ IandI/multipleinheritance.html:
It is true that Java did not use to support multiple inheritance of implementation (just of type i.e. interface). That was a design decision.
However, since Java 8, it supports multiple inheritance using default methods. See http://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html:
当多个父类定义自己的某些实现,并且这两个父类的子类必须处理使用哪个实现的歧义时,就会出现钻石问题。 那么,如果 Java 中的所有类都派生自 Object,那又怎么样,那就是单个父类。 “单父类,多个派生类”与“多父类,单个派生类”不同
The diamond problem arises when multiple parent classes define their own implementations of something and the child class of these two has to deal with the ambiguity of which implementation to use. So what if all classes in Java derive from Object, that's a single parent class. "Single parent, multiple derived classes" is not the same as "Multiple parents, single derived class"