Java 中的多重继承是如何工作的?

发布于 2024-09-13 20:40:46 字数 120 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

霞映澄塘 2024-09-20 20:40:46

超类与父类不同。你只能有一位母亲,但你有更多的女性祖先。

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.

零時差 2024-09-20 20:40:46

正如其他人所解释的,Java 不支持多重继承。
但是,当您实现多个接口时,您可以(某种程度上)拥有多重继承:

interface Moveable {
    void relocate(Coordinate position);
    Coordinate getCurrentPos();
}

interface Tradeable {
    void sell(BigInteger amount);
    void buy(BigInteger amount);
}

interface Crashable {
    void crash();
}

class Vehicle implements Moveable, Tradeable, Crashable {

}

现在 Vehicle 应该来自它实现的接口的所有方法。

Java doesn't support multiple inheritance, as everyone else explained.
But you can (kind of) have multiple inheritance when you implement multiple interfaces:

interface Moveable {
    void relocate(Coordinate position);
    Coordinate getCurrentPos();
}

interface Tradeable {
    void sell(BigInteger amount);
    void buy(BigInteger amount);
}

interface Crashable {
    void crash();
}

class Vehicle implements Moveable, Tradeable, Crashable {

}

Now Vehicle should all methods from the interfaces it implements.

强辩 2024-09-20 20:40:46

不,Object 只是您创建的任何类的最终父类,

多重继承意味着您可以编写一个扩展 StringInteger 的类例如,并获得每个的属性。这是 Java 无法做到的。如果这是您想做的事情,您可能需要查看委托模式

No, Object will just be the eventual parent class of any class you create

Multiple inheritence would mean you could write a class that extends String and Integer 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

夜雨飘雪 2024-09-20 20:40:46

不,这只是继承,照常

grandparent

parent

child

孩子只有一个父母,
并且父级有一个祖父母(没有逻辑意义,但无论如何:)

当您从两个不同的类继承时,多重继承不需要彼此有任何关系

donkey      car

     donkeycar

(正如您已经指出的,这在java中是不可能的)

no, its just inheritance, business as usual

grandparent

parent

child

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

donkey      car

     donkeycar

(as you already noted, its not possible in java)

想挽留 2024-09-20 20:40:46

你的对象的超类也有一个超类等等。

所有对象形成一棵树,以 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.

清风夜微凉 2024-09-20 20:40:46

不。

多重继承意味着您可以从两个类继承,

class A {}

class B {}

class C extends A, B {}

而这在 Java 中是不可能的。

你能做的是

class A {}

class B extends A {}

class C extends B {}

,你有多个超类,但只有一个父类。

No.

The multiple inheritance mean that You inherit for example from two classes

class A {}

class B {}

class C extends A, B {}

and this is not possible in Java.

What You can do is

class A {}

class B extends A {}

class C extends B {}

So You have more then one super class but only one parent.

表情可笑 2024-09-20 20:40:46

我的理解是,多重继承是水平工作的(多个父超类直接继承到一个子类中),而不是垂直工作(父类的父类),考虑继承树。

正如你所说,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.

若能看破又如何 2024-09-20 20:40:46

继承是传递性的。您扩展一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文