一个类如何扩展 Java 中的两个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17241583/
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
How can a class extend two classes in Java?
提问by splitgames
If Java can only extend one class, and every class extends java.lang.Object
, how can it extend another class? And how come every class doesn't have written extends Object
?
如果 Java 只能扩展一个类,并且每个类都扩展java.lang.Object
,它如何扩展另一个类?怎么每节课都没有写extends Object
?
回答by NINCOMPOOP
If java can only extend one class, and every class extends java.lang.Object, how can it extend another class?
如果java只能扩展一个类,并且每个类都扩展java.lang.Object,那么它如何扩展另一个类?
When you say A extends B then it means that A extends B and B extends Object
. One class can inherit from another which can inherit from another and at the top of the chain is java.lang.Object
. Java doesn't support multiple inheritance , but supports multi-levelinheritance.
当你说 A extends B 时,它意味着 A extends B 和 B extends Object
。一个类可以从另一个类继承,另一个类可以从另一个类继承,并且在链的顶部是java.lang.Object
. Java 不支持多重继承,但支持多级继承。
how come every class doesn't have written "extends Object" ?
为什么每个班级都没有写“扩展对象”?
All classes extend Object
implicitly , so it will be redundant coding.
所有类都Object
隐式扩展,因此将是冗余编码。
回答by user2509983
Since java.lang.Object is the parent of every object in Java, you don't have to explicitly write extends Object
由于 java.lang.Object 是 Java 中每个对象的父对象,因此您不必显式编写 extends Object
You can always extend any class. If you need to have behavior of 2 classes - you need to use interfaces for that
您始终可以扩展任何类。如果您需要具有 2 个类的行为 - 您需要为此使用接口
回答by kdureidy
Every class in Java extends implicitlyclass Object
, and you only have the permission to extend one more class, and infinite number of interfaces.
Java 中的每个类都隐式地扩展了class Object
,您只能再扩展一个类,以及无限数量的接口。
Same as having one default constructor for each class if you didn't write a constructor.
如果您没有编写构造函数,则与为每个类拥有一个默认构造函数相同。
So when you say
所以当你说
class A{
}
it is actually looks like this:
它实际上是这样的:
class A extends Object{
A(){
super();
}
}
But when you say when you say
但是当你说当你说
class B extends A
In this case class B will have the features of class A, and because A extends Object, class B will also inherit class Object.
在这种情况下,B 类将具有 A 类的特性,并且由于 A 扩展了 Object,B 类也将继承 Object 类。
A -> Object
B -> A
So
B -> Object
A -> 对象
B -> A
所以
B -> 对象
回答by Priyanka.Patil
The one and only that you are allowed to extend also extends the class Object
ultimately.Hence you are not extending twice.
您被允许扩展的唯一一个Object
最终也会扩展该类。因此您不会扩展两次。
回答by Devolus
Multiple inheritance is not supported in Java, you can only inherit in a straight line (so to speak). You use interfaces though, which are IMO much more powerfull. I never needed multiple inheritance, but I make heavy use of interfaces. Basically instead of creating a second class, you define an Interface, and then let some class implement it. This can be a different class or the same.
Java 不支持多重继承,只能直线继承(可以这么说)。不过,您使用的接口是 IMO 更强大的接口。我从不需要多重继承,但我大量使用接口。基本上不是创建第二个类,而是定义一个接口,然后让某个类实现它。这可以是不同的类,也可以是相同的。
All classes are already derived from Objects, you don't need to write that specifically.
所有的类都已经从 Objects 派生出来,你不需要专门编写它。
回答by Dallas
Each of the following can extend the object to the left:
Object -> Vehicle -> Car -> Coupe -> Camaro
以下每个都可以将对象扩展到左侧:
对象 -> 车辆 -> 汽车 -> 双门轿车 -> Camaro
So for example, Coupe
is extending Car
, and it is not (directly) extending Object
... it's just that if you keep going up the chain you'll eventually get to Object
.
因此,例如,Coupe
正在扩展Car
,而不是(直接)扩展Object
......只是如果您继续沿链向上,您最终会到达Object
.
You couldn't however, have Coupe
extend both Car
and Truck
但是,您不能Coupe
同时扩展Car
和Truck
回答by cevaris
Here is an example showing the class hierarchy of Java's java.lang.Number class.
这是一个示例,显示了 Java 的 java.lang.Number 类的类层次结构。
http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
As you can see, Integer class inherits from Number, and Number inherits from Object. So you can inherit multiple classes by chaining them.
可以看到,Integer类继承自Number,Number类继承自Object。所以你可以通过链接它们来继承多个类。
回答by Zzz
In object-oriented programming (OOP), inheritance describes a relationship between two types, or classes, of objects in which one is said to be a subtype or child of the other.
在面向对象编程 (OOP) 中,继承描述了对象的两种类型或类之间的关系,其中一种被称为另一种的子类型或子类型。
A -> B -> C -> D
The child inherits features of the parent, allowing for shared functionality. For example, one might create a variable class Animal with features such as eating etc.; then define a subtype Dog that inherits those features without having to explicitly program them, while adding new features like chasing cats.
子级继承父级的功能,允许共享功能。例如,可以创建一个具有诸如吃等特征的变量类 Animal;然后定义一个子类型 Dog 来继承这些功能,而无需显式编程它们,同时添加诸如追猫之类的新功能。
Multiple inheritance allows programmers to use more than one hierarchy simultaneously, such as allowing Dog to inherit from Robot character and Animal and access features from within all of those classes.
多重继承允许程序员同时使用多个层次结构,例如允许 Dog 继承 Robot 角色和 Animal 并访问所有这些类中的功能。
A
/ \
C D
\ /
E
Java uses the first model and therefore inheriting from object and another class doesn't cause a problem because it is hierarchical in nature.
Java 使用第一个模型,因此从对象和另一个类继承不会导致问题,因为它本质上是分层的。
Object -> Animal -> Dog -> Pit Bull
The reason that every object does not need to extend Object is because it happens implicitly.
每个对象不需要扩展 Object 的原因是因为它是隐式发生的。