Java does not allow multiple inheritance, meaning that a class cannot inherit from two classes, which does not have anything in common, meaning that they are not on the same inheritance path. However, a class can inherit from more classes, if these classes are super classes of the direct super class of the class. But the class inherits from these classes indirectly, meaning that it does not "see" anything from these upper super classes, right? I was confused when considering constructors (using super() in the constructor). For example, if we have the following classes:

Java不允许多重继承,这意味着一个类不能从两个类继承,这两个类没有任何共同点,这意味着它们不在同一个继承路径上。但是,如果这些类是类的直接超类的超类,则类可以从更多类继承。但是这个类间接地从这些类继承,这意味着它不会从这些超级类中“看到”任何东西,对吧?考虑构造函数时我很困惑(在构造函数中使用super())。例如,如果我们有以下类:

public class A {
    public A() { 
      .... 
    }
}

public class B extends A {
    public B() {
      super();
      ....
    }
}

public class C extends B {
    public C() {
      super();
      ....
   }
}

the constructor of class C invokes first the constructor of class B using super(). When this happens, the constructor of B itself invokes first the constructor of A (with super()), but the constructor of C does not know anything about the constructor of A, right? I mean, the inheritance is only from the direct super class - the first (nearest) class from the inheritance hierarchy. This is my question - with super() we mean only the constructor of the direct super class, no matter how many other classes we have in the inheritance hierarchy. And this does not apply only for constructors, but for any methods and instance variables..

C类的构造函数首先使用super()调用类B的构造函数。当发生这种情况时,B本身的构造函数首先调用A的构造函数(带有super()),但是C的构造函数对A的构造函数一无所知,对吧?我的意思是,继承只来自直接超类 - 继承层次结构中的第一个(最近的)类。这是我的问题 - 使用super()我们只是指直接超类的构造函数,无论我们在继承层次结构中有多少其他类。这不仅适用于构造函数,也适用于任何方法和实例变量。

Regards

5 个解决方案

#1


You have to invoke some constructor in your immediate base class. This can be

您必须在直接基类中调用一些构造函数。这可以

public class A {
     public A() { 
      .... 
     }
    public A(String foo) { 
      .... 
    }
}

public class B extends A {
    public B() {
        super();
        .. or ..
        super("ThisIsAB")
    }
}

public class C extends B {
    public C() {
      super();
      ....
   }
}

So for constructors you cannot AVOID constructing your intermediate base classes, but you can choose which constructor to use. If there is only the no-args constructor it's all handled for you with an implicit call to super. With multiple constructors you have some more choices.

因此,对于构造函数,您无法AVOID构造中间基类,但您可以选择使用哪个构造函数。如果只有no-args构造函数,则会通过对super的隐式调用来处理它。使用多个构造函数,您可以获得更多选择。

super can refer to any non-private variable or method in any base class. So methods and variables are not the same as constructors in this respect.

super可以引用任何基类中的任何非私有变量或方法。因此,方法和变量在这方面与构造函数不同。

更多相关文章

  1. java中的成员变量和局部变量的区别
  2. JavaScript中的map()函数
  3. 如何在另一个类中使用静态类中的变量?
  4. 自定义MapReduce输入格式 - 找不到构造函数
  5. 关于webview中java调用js函数(解决loadUrl函数没反应)
  6. 使用Java中的Scala:将函数作为参数传递
  7. JAVA 静态方法和静态变量和final和※静态import※
  8. Linux(Centos7.X ) 配置Java 环境变量
  9. java通过映射取得方法对一个类的变量进行赋值

随机推荐

  1. Android内存泄露调试分享
  2. Android(安卓)ContextMenu
  3. android EditText彻底搞定输入框隐藏/显
  4. Android学习笔记1——建立android工程及
  5. [Android] 该文件包与具有同一名称的现有
  6. Fragment API将兼容Android 1.6到3.0
  7. (原创)Android入门教程(六)之------使用a
  8. OpenCV4 --Android 环境配置【详细教程】
  9. Android 数字版权保护播放器开发
  10. Android(安卓)离线安装宝典