java中的构造函数
public class A {
public A() {
System.out.println("a1");
}
public A(int x) {
System.out.println("a2");
}}
public class B extends A {
public B() {
super(5);
System.out.println("b1");
}
public B(int x) {
this();
System.out.println("b2");
}
public B(int x, int y) {
super(x);
System.out.println("b3");
}}
我不明白为什么当我运行 B b= new B();
B 扩展 A 时不应用 A 的默认构造,所以首先我们调用 A 的构造来打印“a1 ”,然后我们调用 A 的第二个结构,它打印“a2”,而 B()
打印“b1”,但是当我运行它时,它只打印“a2,b1”,所以显然A()
一开始就没有应用 - 为什么?
public class A {
public A() {
System.out.println("a1");
}
public A(int x) {
System.out.println("a2");
}}
public class B extends A {
public B() {
super(5);
System.out.println("b1");
}
public B(int x) {
this();
System.out.println("b2");
}
public B(int x, int y) {
super(x);
System.out.println("b3");
}}
I don't understand why the default constructure of A is not applied when I run B b= new B();
B extends A, so First we call the constrcture of A that supposed to print "a1", and then we call the the second constructure of A which prints "a2" and B()
prints "b1", but when I run it, it prints only "a2,b1", so obviously A()
wan't applied at the beginning- why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您说
B b = new B();
时,您正在调用默认构造函数,因为它已经调用了其超级构造函数 [
super(5)
],所以编译器不会插入隐式默认构造函数。于是就有了结果。注意:从您的问题来看,您似乎认为创建对象时会调用所有构造函数。恐怕这是不正确的。只有您显式调用来创建对象的构造函数才会被调用。如果该构造函数通过
this()
方法调用其他构造函数,则只有其他构造函数才会被调用。When you say
B b = new B();
you are calling the default constructor which isSince this already has a call to its super constructor [
super(5)
] so the compiler will not insert the implicit default constructor. Hence the result.NOTE: From your question, it seems that you have the idea that all the constructors are called when you create an object. I am afraid that is incorrect. Only that constructor will be called which you explicitly call to create the object. And if that constructor calls other constructor via the
this()
method, only then the other constructors will be called.这个说法是不正确的。
在类 B 中,您的无参数构造函数
调用带有 int 参数的超类(类 A)的构造函数。
您永远不会调用 super(),因此当您调用 B 的任何构造函数时,不会调用打印“a1”的构造函数
。 调用超级构造函数必须是构造函数的第一行。如果你想调用超类的无参数构造函数(在本例中,是打印“a1”的构造函数),你可以写...
如果你没有指定调用超类构造函数,那么java将自动放入一个调用到无参数超级构造函数。
This statement is incorrect.
In class B your no arguments constructor
calls the constructor of the superclass (class A) which takes an int parameter.
You never make a call to super() so the constructor that prints "a1" will not be called when you call any of B's constructors
Calling a super constructor must be the first line of a constructor. If you wish to call the no argument constructor of a superclass (in this case, the one that prints "a1"), you would write...
If you do not specify calling a super constructor, then java will automatically put in a call to the no argument super constructor.
这是因为您在 B 构造函数中调用 super(5) ,它调用第二个 A 构造函数而不是第一个构造函数。
That's because you call super(5) in B constructor which call the second A constructor instead of the first one.
我认为你误解了 Java 如何处理构造函数。首先,默认情况下,Java 将为每个类仅调用一个构造函数,除非您使用
this(...)
明确告诉它调用更多构造函数。其次,被调用的一个构造函数是超类的默认构造函数(因此它将调用super()
,而不是this()
);所以类A
实际上看起来像这样:因此调用
A()
将隐式调用Object()
,但是调用A(int x )
将也隐式调用Object()
,而不是 - 您似乎假设的 -A()
。由于在 B 中您总是显式指定调用另一个构造函数,因此编译器不会添加任何内容。下面我添加了关于调用
super(...)
和this(...)
时会发生什么情况的注释,所以再次强调,要记住的重要一点是 Java将在任何构造函数的第一行插入对 super() 的调用,除非您使用
this(...)
或super(. ..)
。它绝不会单独插入this()
。I think you missunderstand how Java handles constructors. First of all, by default Java will call only one constructor per class, unless you explicitly tell it to call more using
this(...)
. Secondly, that one construct that is called is the default constructor of the super class (so it will callsuper()
, notthis()
); so classA
actually looks like this:So invoking
A()
will implicitly callObject()
, however invokingA(int x)
will also implicitly callObject()
, and not - how you seem to assume -A()
.Since in B you always explicitly specify to call another constructor, the compiler will not add anything. Below I added comments on what will happen on the calls to
super(...)
andthis(...)
So again, the important thing to remember is that Java will insert a call to
super()
in the first line of any constructor, unless you explicitly invoke another constructor usingthis(...)
orsuper(...)
. It will never insertthis()
by itself.