Java 中的多重继承是如何工作的?
Object
类是类层次结构的根。每个类都有 Object
作为超类。那么,如果我扩展一个 API 类,它会像多重继承吗?显然,Java不支持多重继承。那么它是如何运作的呢?
Class Object
is the root of class hierarchy. Every class has Object
as a superclass. So, if I am extending a API class, will it be like, multiple inheritance? Obviously, Java doesn't support multiple inheritance. How does it then work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
超类与父类不同。你只能有一位母亲,但你有更多的女性祖先。
Superclass is not the same thing as parent class. You can only have one mother, but you have a much larger number of female ancestors.
正如其他人所解释的,Java 不支持多重继承。
但是,当您实现多个接口时,您可以(某种程度上)拥有多重继承:
现在
Vehicle
应该来自它实现的接口的所有方法。Java doesn't support multiple inheritance, as everyone else explained.
But you can (kind of) have multiple inheritance when you implement multiple interfaces:
Now
Vehicle
should all methods from the interfaces it implements.不,
Object
只是您创建的任何类的最终父类,多重继承意味着您可以编写一个扩展
String
和Integer
的类例如,并获得每个的属性。这是 Java 无法做到的。如果这是您想做的事情,您可能需要查看委托模式No,
Object
will just be the eventual parent class of any class you createMultiple inheritence would mean you could write a class that extends
String
andInteger
for example, and gains the properties of each. This cannot be done with Java. You probably want to look at the Delegate pattern if this is the sort of thing you want to do不,这只是继承,照常
孩子只有一个父母,
并且父级有一个祖父母(没有逻辑意义,但无论如何:)
当您从两个不同的类继承时,多重继承不需要彼此有任何关系
(正如您已经指出的,这在java中是不可能的)
no, its just inheritance, business as usual
the child only has one parent,
and the parent has a grandparent (doesnt make logical sense, but whatever :)
multiple inheritance would be when you inherit from two different classes does not need to have anying do with each other
(as you already noted, its not possible in java)
你的对象的超类也有一个超类等等。
所有对象形成一棵树,以 java.lang.Object 作为根节点。
The super class of your object also has a super class and so on.
All objects form a tree with java.lang.Object as it's root node.
不。
多重继承意味着您可以从两个类继承,
而这在 Java 中是不可能的。
你能做的是
,你有多个超类,但只有一个父类。
No.
The multiple inheritance mean that You inherit for example from two classes
and this is not possible in Java.
What You can do is
So You have more then one super class but only one parent.
我的理解是,多重继承是水平工作的(多个父超类直接继承到一个子类中),而不是垂直工作(父类的父类),考虑继承树。
正如你所说,Java 中只允许垂直类型。
My understanding of it it that multiple inheritance works horizontally (multiple parent superclasses inherited directly into one subclass) rather than vertically (parents of parents), thinking of the inheritance tree.
As you say, only the vertical kind is allowed in Java.
继承是传递性的。您扩展一个 API 类,该 API 类本身也扩展了 Object。所以你间接扩展了 Object.更短地说,如果 A 扩展 B 并且 B 扩展 C,那么 A 扩展 C。
这不是多重继承。如果你愿意的话,“继承链”可以有多长。但最终,一切都有对象。
真正的多重继承将同时从两个不相关的类扩展。在 Java 中你无法做到这一点。
Inheritance is transitive. You extend an API class, and that API class itself extends Object. So you're indirectly extending Object. Shorter, if A extends B and B extends C, then A extends C.
This is not multiple inheritance. The 'inheritance chain', if you will, can be as long as you want it to be. But in the end, everything has Object at the end of it.
Real multiple inheritance would be extending from two unrelated classes at once. You cannot do this in Java.