当前位置: 技术问答>java相关
急,请问,java执行流程的顺序,谢谢!!!!
来源: 互联网 发布时间:2015-03-12
本文导语: Determine the result of attempting to compile and run the following code: class A { public int Avar; public A() { System.out.println("AAA"); doSomething(); } public void doSomething() { Avar = 1111; ...
Determine the result of attempting to compile and run the following code:
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
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 B();
}
}
The output is:
AAA
Bvar=0
BBB
Bvar=2222
Avar=0
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
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 B();
}
}
The output is:
AAA
Bvar=0
BBB
Bvar=2222
Avar=0
|
class A {
public int Avar;
4==> public A() {
5==> System.out.println("AAA");
6==> doSomething();
}
never run==> public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends 3==> A {
8==> public int Bvar = 2222;
9==> public B() {
10==> System.out.println("BBB");
11==> doSomething();
13==> System.out.println("Avar=" + Avar);
}
7==>12==> public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
1==> public static void main(String[] args) {
2==> new B();
}
}
public int Avar;
4==> public A() {
5==> System.out.println("AAA");
6==> doSomething();
}
never run==> public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends 3==> A {
8==> public int Bvar = 2222;
9==> public B() {
10==> System.out.println("BBB");
11==> doSomething();
13==> System.out.println("Avar=" + Avar);
}
7==>12==> public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
1==> public static void main(String[] args) {
2==> new B();
}
}
|
根据你的结果的解释:
1,B extends A 执行A的构造方法,
2,dosomething() 被执行
3,
1,B extends A 执行A的构造方法,
2,dosomething() 被执行
3,