java 为什么总是调用超类构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34488484/
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
Why is super class constructor always called
提问by rgarci0959
I have the following 2 classes
我有以下两门课
public class classA {
classA() {
System.out.println("A");
}
}
class classB extends classA {
classB() {
System.out.println("B");
}
}
and then running
然后运行
1
1
classA c = new classB();
or
或者
2
2
classB c = new classB();
always gives
总是给
A
B
Why is this happening? At first glance, in either scenario, I would assume that only the classB
constructor would be called and thus the only output would be
为什么会这样?乍一看,在任何一种情况下,我都会假设只会classB
调用构造函数,因此唯一的输出是
B
but this is clearly wrong.
但这显然是错误的。
回答by Aniket Thakur
That is how Java works. The constructors of the parent classes are called, all the way up the class hierarchy through Object
, before the child class's constructor is called.
这就是 Java 的工作方式。在调用Object
子类的构造函数之前,父类的构造函数被调用,一直到类层次结构。
Quoting from the docs:
引用文档:
With
super()
, the superclass no-argument constructor is called. Withsuper(parameter list)
, the superclass constructor with a matching parameter list is called.Note:If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.
Object
does have such a constructor, so ifObject
is the only superclass, there is no problem.If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of
Object
. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
使用
super()
,调用超类无参数构造函数。使用super(parameter list)
,调用具有匹配参数列表的超类构造函数。注意:如果构造函数没有显式调用超类构造函数,Java 编译器会自动插入对超类的无参数构造函数的调用。如果超类没有无参数构造函数,您将收到编译时错误。
Object
确实有这样的构造函数,所以如果Object
是唯一的超类,没有问题。如果子类构造函数显式或隐式调用其超类的构造函数,您可能会认为将调用整个构造函数链,一直返回到 的构造函数
Object
。事实上,情况正是如此。它被称为构造函数链接,当有很长的类下降时,你需要注意它。
回答by Alex Suo
Super class constructor is always called during construction process and it's guaranteed that super class construction is finished before subclass constructor is called. This is the case for most if not all the object oriented language. You could explicitly call super class constructor with parameter if you don't want to invoke the default constructor; otherwise such call is automated by compiler.
超类构造函数总是在构造过程中被调用,并保证在调用子类构造函数之前完成超类构造。大多数(如果不是全部)面向对象语言都是这种情况。如果不想调用默认构造函数,可以显式调用带参数的超类构造函数;否则这样的调用是由编译器自动执行的。
回答by Sabir Khan
There is no difference in both statements in terms of objects being constructed so you see same out put.
两种语句在构造对象方面没有区别,因此您会看到相同的输出。
Just using a different reference type on left hand sidewhile constructing same objects using new
is not going to make any difference as far as object creation and constructor chaining is concerned.
就对象创建和构造函数链接而言,仅在左侧使用不同的引用类型,同时使用构造相同的对象new
不会产生任何区别。
Whatever difference is there among two of your statements is after objects get created.
无论您的两个语句之间有什么区别,都是在创建对象之后。