Java 每个类都扩展对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19190031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:54:47  来源:igfitidea点击:

Every class extends Object?

javainheritance

提问by user2848012

I read that every class extends a object class by default. then it should cause multiple inheritence and must not be supported by Java.

我读到每个类都默认扩展一个对象类。那么它应该会导致多重继承并且Java不支持它。

Please search a lot but could not get proper answer/how compiler works in this case. please help me out.

请搜索很多,但无法得到正确的答案/在这种情况下编译器是如何工作的。请帮帮我。

回答by Sotirios Delimanolis

How can it be multiple inheritance?

怎么可能是多重继承?

class Object { /* stuff */ }

class Foo /* implicit extends Object */ {}

class FooBar extends Foo /* and therefore extends Object */ {}

The rules are described in the Java Language Specification:

Java 语言规范中描述了这些规则

The class Object is a superclass (§8.1.4) of all other classes.

Object 类是所有其他类的超类(第 8.1.4 节)。

回答by arshajii

When you have something like

当你有类似的东西

class A {}

then Aimplicitly extends Object. In fact the bytecode resembles

然后A隐式扩展Object。事实上,字节码类似于

class A extends Object {}

Now, if we have

现在,如果我们有

class B extends A {}

Then Bextends Abut is alsoa subclass of Object, since Ais a subclass of Object. This is not, however, multiple inheritance:

然后B扩展A也是的子类Object,因为A是 的子类Object。然而,这不是多重继承:

Object
  |
  A
  |
  B

Multiple inheritance would look like this:

多重继承看起来像这样:

Object   A
  \     /
   \   /
    \ /
     B

i.e. Binheriting from two hierarchically unrelated classes.

B从两个层次无关的类继承。

回答by eyettea

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. Java documentation

类对象是类层次结构的根。每个类都有一个 Object 作为超类。所有对象,包括数组,都实现了这个类的方法。 Java 文档

回答by Barryrowe

Your confusion seems to be in the meaning of the term "Multiple Inheritance." Multiple inheritance refers to when an object can be a DIRECT descendant of more than one other type.

您的困惑似乎与“多重继承”一词的含义有关。多重继承是指一个对象可以是多个其他类型的 DIRECT 后代。

This would be an example of multiple inheritance, and is not supported in Java:

这将是多重继承的一个例子,在 Java 中不受支持:

class Z extends A, B, C{

}

回答by La-comadreja

Multiple inheritance means that a class inherits from multiple classes that are not in the same line of inheritance. The problem with multiple inheritance is that if methods and instance variables have the same name but different operations or values, it is not clear which inherited method or variable is the one to use.

多重继承是指一个类继承自多个不在同一继承行中的类。多重继承的问题在于,如果方法和实例变量具有相同的名称但操作或值不同,则不清楚使用哪个继承的方法或变量。

For example:

例如:

class Foo {
    void baz() { }
}
class Bar {
    void baz() { System.out.println("test"); }
}
class FooBar extends Foo extends Bar {}

is an example of multiple inheritance. If an object of class FooBar called baz(), we would not know which baz() to use.

是多重继承的一个例子。如果 FooBar 类的对象调用 baz(),我们将不知道使用哪个 baz()。

However, it is not multiple inheritance if class FooBar only extends class Foo, but not class Bar (and class Foo extends class Object). If a method in Object, such as toString() were to be written in Foo, such a method would override Object's toString() i.e. class FooBar would know that the child class, Foo's toString() should be called and not the parent class, Object's toString().

但是,如果类 FooBar 仅扩展类 Foo 而不是类 Bar(并且类 Foo 扩展类 Object),则它不是多重继承。如果 Object 中的一个方法,例如 toString() 被写在 Foo 中,这样的方法将覆盖 Object 的 toString() 即类 FooBar 会知道子类,Foo 的 toString() 应该被调用而不是父类,对象的 toString()。

回答by Nick

When you look at a class, for example A, looking up the inheritance tree will always return you to Object. If there are multiple classes between Aand Object, this is not multiple inheritance. This can be drawn like so:

例如A,当您查看一个类时,查找继承树将始终返回到Object. 如果A和之间有多个类Object,则这不是多重继承。这可以像这样绘制:

Object
|
SomeIntermediateClass
|
A

Multiple inheritance occurs when a single class is the child of at least two "branches" in an inheritance tree. For example:

当单个类是继承树中至少两个“分支”的子类时,就会发生多重继承。例如:

Object
|   |
A   B
 \ /
  C

In the above case, Cis inheriting from multiple parents, and is thus involved in multiple-inheritance.

在上面的例子中,C是从多个父代继承的,因此涉及到多重继承。