Java继承——构造函数
在准备期末考试时,我在我目前正在学习的书中看到了以下陈述。考虑以下代码:
class A {
public A(int x) { }
}
class B extends A {
public B(int x ) { }
}
是否必须在类 B(super(x)) 的构造函数中调用类 A 的构造函数。书中指出这不是强制性的,因为它们具有确切的参数数量和类型。但是当我在 java 编译器中尝试这个时,会抛出以下错误:
A 类中的构造函数 A 不能 适用于给定类型;必需的: 找到 int:没有参数原因: 实际和形式参数列表 长度不同
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 :
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编译器会自动在开头插入
super()
。然而,即使构造函数参数,
super()
(不带参数)也会被添加,它会调用超类的默认构造函数。而你没有,因此会出现错误。您必须指定
super(x)
(以调用A(x)
),或定义一个无参构造函数。顺便说一下,Eclipse 编译器给出了更好的错误消息:
The compiler automatically inserts
super()
in the beginning.However, even constructors arguments,
super()
(without arguments) is added which invokes the default constructor of the superclass. And you don't have one, hence the error.You have to specify
super(x)
(to invokeA(x)
), or define a no-argument constructor.By the way, Eclipse compiler gives a way better error message:
看起来编译器尝试使用
super()
创建对超类默认构造函数的调用,但该调用不可用:但是回到你的书:我从未听说过可以这样做的规则如果实际构造函数与直接超类中的构造函数具有完全相同的参数列表,则跳过构造函数中的
super
语句。仅隐式添加对超类默认构造函数的调用 (super()
),但这要求超类具有一个默认构造函数。与您书中所写的内容相反(或与您对书面文本的理解相反),这是语言规范中的一句话:
It looks like the compiler tries to create a call to the superclasses default constructor with
super()
, which isn't avaliable:But back to your book: I've never heard of a rule that you can skip the
super
statement 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 has a default constructor.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:
如果您的基类具有默认构造函数(无参构造函数),则当您扩展
B
时,您不需要显式调用super()
,因为它被称为任何方式。但是当你有一个带参数的构造函数时,在
B
中制作带参数的构造函数时,需要传入super()
一个A
的参数>示例:
If you have a the base class having a default constructor (no-arg constructor), when you extend
B
, you don't need to explicitly callsuper()
because it is called any way.But when you have a constructor with arguments, when making the contructor with parameters in
B
, you need to pass insuper()
a parameter forA
example :
当您没有默认构造函数并且使用默认构造函数创建实例时,就会发生这种情况。因为如果你有任何参数化构造函数,那么编译器不会为你插入默认的构造函数,而是你必须定义。
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.
对于Java,需要调用超类的构造函数。因此,每当您生成子类的构造函数时,超类的构造函数都是由 IDE 自行创建的。
这是因为每当基类构造函数需要参数时,编译器都会认为它们将由基类构造函数填充。如果是默认构造函数,就可以了。无需在子类中调用super()。
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.
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.