Java 继承 - 构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6248809/
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-10-30 15:01:38  来源:igfitidea点击:

Java inheritance - constructors

javainheritanceconstructor

提问by Colin Dumitru

While studying for my finals, I came across the following statement in the book from which I am currently studying. Considering the following code :

在为期末考试而学习时,我在我目前正在学习的书中看到了以下陈述。考虑以下代码:

class A {
    public A(int x) {   }
}

class B extends A {
    public B(int x ) {   }
}

is it mandatory to call the constructor of class A in the constructor of class B(super(x)). The book states that it's not mandatory, because they have the exact number and type of parameters. But when I try this in a java compiler, the following error gets thrown :

在类B(super(x))的构造函数中调用A类的构造函数是不是必须的。这本书指出这不是强制性的,因为它们具有确切的参数数量和类型。但是当我在 java 编译器中尝试这个时,会抛出以下错误:

constructor A in class A cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length

类 A 中的构造函数 A 不能应用于给定类型;要求:int found:无参数原因:实际和形式参数列表的长度不同

回答by Bozho

The compiler automatically inserts super()in the beginning.

编译器自动super()在开头插入。

However, even constructors arguments, super()(without arguments) is added which invokes the defaultconstructor of the superclass. And you don't have one, hence the error.

然而,即使是构造函数参数,super()(没有参数)也会被添加,它会调用超类的默认构造函数。而你没有一个,因此是错误的。

You have to specify super(x)(to invoke A(x)), or define a no-argument constructor.

您必须指定super(x)(调用A(x))或定义无参数构造函数。

By the way, Eclipse compiler gives a way better error message:

顺便说一下,Eclipse 编译器给出了一个更好的错误信息:

Implicit super constructor A() is undefined. Must explicitly invoke another constructor

隐式超级构造函数 A() 未定义。必须显式调用另一个构造函数

回答by Andreas Dolk

It looks like the compiler tries to create a call to the superclasses default constructor with super(), which isn't avaliable:

看起来编译器试图使用 来创建对超类默认构造函数的调用super(),这不可用:

required: int
found:    no arguments

But back to your book: I've never heard of a rule that you can skip the superstatement in a constructor if the actual constructor has the exact same parameter list as a constructor in the direct superclass. Only a call to the superclass's default constructor is added implicitly (super()) but that requires that the superclass hasa default constructor.

但是回到你的书上:我从来没有听说过super如果实际构造函数与直接超类中的构造函数具有完全相同的参数列表,那么你可以跳过构造函数中的语句的规则。仅隐式添加对超类的默认构造函数的调用 ( super()) 但这要求超类具有默认构造函数。

In contrast to what's written in your book (or in contrast to your understanding of the written text), here's a sentence from the language spec:

与您书中所写的内容相反(或与您对书面文本的理解相反),这是语言规范中的一句话:

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a super class constructor invocation “super();”, an invocation of the constructor of its direct superclass that takes no arguments.

如果构造函数体不以显式构造函数调用开始,并且被声明的构造函数不是原始类 Object 的一部分,则编译器隐式假定构造函数体以超类构造函数调用“super();”开始。,调用其直接超类的构造函数,不带参数。

回答by Andrei Sfat

If you have a the base class having a default constructor (no-arg constructor), when you extend B, you don't need to explicitly call super()because it is called any way.

如果您有一个具有默认构造函数(无参数构造函数)的基类,当您扩展时B,您不需要显式调用,super()因为它以任何方式调用。

But when you have a constructor with arguments, when making the contructor with parameters in B, you need to pass in super()a parameter for A

但是当你有一个带参数的构造函数时,在构造带参数的构造函数时B,你需要传入super()一个参数 forA

example :

例子 :

class A {
    public A(int x) {   }
  }

  class B extends A {
    public B(int x ) 
    {
       super(x); // need to specify the parameter for class A
       //... 
    }
  }

回答by GuruKulki

This happens when you dont have a default constructor and you are creating an instance with default one. Because If you have any Parameterized constructor then compiler will not insert the default one for you, instead you have to define.

当您没有默认构造函数并且使用默认构造函数创建实例时,就会发生这种情况。因为如果你有任何参数化的构造函数,那么编译器不会为你插入默认的构造函数,而是你必须定义。

回答by Stuti

It is neccessary to call constructor of super class in case of Java. Therefore, whenever you generate constructor of sub class, super class' constructor is self created by IDE.

在 Java 的情况下,必须调用超类的构造函数。因此,无论何时生成子类的构造函数,超类的构造函数都是 IDE 自己创建的。

It is because whenever base class constructor demands arguments, compiler thinks they are to be filled by base class constructor. In case of default constructor, it is OK. No need to call super() in sub class.

这是因为每当基类构造函数需要参数时,编译器都认为它们将由基类构造函数填充。在默认构造函数的情况下,它是可以的。无需在子类中调用 super()。