当前位置: 技术问答>java相关
[oo问题大讨论] 关于构造器调用的怪题!
来源: 互联网 发布时间:2015-03-16
本文导语: class A { public int Avar; public A() { System.out.println("AAA"); doSomething(); } public void doSomething() { Avar = 1111; System.out.println("A.doSomething()"); } } public class Test1 extends A { ...
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class Test1 extends A {
public int Bvar = 2222;
public Test1() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new Test1();
}
}
如果这样运行,结果是:
AAA
Bvar=0
BBB
Bvar=2222
Avar=0
如果屏蔽掉类A的默认构造器:变成下面这种样子:
class A {
public int Avar;
/*public A() {
System.out.println("AAA");
doSomething();
}*/
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class Test1 extends A {
public int Bvar = 2222;
public Test1() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new Test1();
}
}
运行结果是:
BBB
Bvar=2222
Avar=0
但是sun的sl-275的培训资料里面是这样讲的:
Often you define a constructor that takes arguments and you want to
use those arguments to control the construction of the parent part of
an object. You can invoke a particular parent class constructor as part
of a child class initialization by “calling” the keyword super from the
child constructor’s first line. To control the invocation of the specific
constructor, you must provide the appropriate arguments to super().
When there is no call to super with arguments, the default parent
constructor (that is, the constructor with zero arguments) is called
implicitly. In this case, if there is no default parent constructor, a
compiler error results.
也就是说在带有参数的构造器调用时,如果没有指明,自动调用super(),但是上例中的b的构造器并不带有参数阿,为什么会调用super ()阿
请各位高手发表意见阿
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class Test1 extends A {
public int Bvar = 2222;
public Test1() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new Test1();
}
}
如果这样运行,结果是:
AAA
Bvar=0
BBB
Bvar=2222
Avar=0
如果屏蔽掉类A的默认构造器:变成下面这种样子:
class A {
public int Avar;
/*public A() {
System.out.println("AAA");
doSomething();
}*/
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class Test1 extends A {
public int Bvar = 2222;
public Test1() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new Test1();
}
}
运行结果是:
BBB
Bvar=2222
Avar=0
但是sun的sl-275的培训资料里面是这样讲的:
Often you define a constructor that takes arguments and you want to
use those arguments to control the construction of the parent part of
an object. You can invoke a particular parent class constructor as part
of a child class initialization by “calling” the keyword super from the
child constructor’s first line. To control the invocation of the specific
constructor, you must provide the appropriate arguments to super().
When there is no call to super with arguments, the default parent
constructor (that is, the constructor with zero arguments) is called
implicitly. In this case, if there is no default parent constructor, a
compiler error results.
也就是说在带有参数的构造器调用时,如果没有指明,自动调用super(),但是上例中的b的构造器并不带有参数阿,为什么会调用super ()阿
请各位高手发表意见阿
|
这个问题我早就问过了
唉,复杂的多态性
你查询一下"关于initializer的"
唉,复杂的多态性
你查询一下"关于initializer的"
|
Patrick_DK说的对,查一下就知道了,我也问过此问题,也是Patrick_DK的帮助,在此多谢了.
|
不客气,大家共同进步:)