简体   繁体   English

Java继承存在疑问

[英]Java inheritance doubt

  1. A class in Java cannot extend more than one class. Java中的类不能扩展多个类。

  2. Every class in Java extends java.lang.Object . Java中的每个类都扩展了java.lang.Object。

  3. From 1 and 2: Any class in Java cannot extend any other class other than java.lang.Object. 从1到2:Java中的任何类都不能扩展除java.lang.Object之外的任何其他类。

What is wrong in this deduction ? 这个扣除有什么问题?

You can only extend one class at a time. 您一次只能扩展一个班级。 But A can extend B can extend C and so on. 但是A可以扩展B可以扩展C等等。

To extend on what Tangens said: 延伸Tangens所说的:

For number 2, it should instead read: 对于2号,它应改为:

Every class that does not explicitly declare a class it extends extends Object 每个没有显式声明它扩展的类的类都扩展了Object

ie if you use the extends keyword, you are now saying that you explicitly extend something other than Object. 即如果你使用extends关键字,你现在说你明确地扩展了Object以外的东西。 However, at some point, that extension path will end up back at Object, if you follow the extension hierarchy. 但是,如果您遵循扩展层次结构,那么在某些时候,该扩展路径将最终返回到Object。

The other part of this is that inheritance is really a chain - you have all of the properties of your parent and their parent and their parent's parent etc. Since the top of this hierarchy is always Object, you must, at one level or another, extend Object. 另一部分是继承实际上是一个链 - 您拥有父级及其父级及其父级父级等的所有属性。由于此层次结构的顶部始终是对象,因此您必须在一个或另一个级别,扩展对象。

Correct statements: 正确的陈述:

  1. A class in Java cannot directly extend more than one class. Java中的类不能直接扩展多个类。

  2. Every class in Java extends java.lang.Object, directly or indirectly. Java中的每个类都直接或间接地扩展了java.lang.Object。

Inheritance is a tree. 继承是一棵树。 A class can only DIRECTLY extend one class, but that class could extend another which extends another, etc. So you can say A extends Object, B extends A, C extends B, etc. C inherits from Object indirectly. 一个类只能直接扩展一个类,但是该类可以扩展另一个类,扩展另一个类等。所以你可以说A扩展Object,B扩展A,C扩展B等.C间接地从Object继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM