下面这个程序运行的结果让人不理解。
运行结果为:
Parent is creating..//不理解
Son is eat: 0//不理解
Son is creating..
Son is eat: 1
主要是前2句不理解。请高手详细说明之。谢谢
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
*Parent.java
*/
public class Parent {
private int eatID=0;
public Parent(){
System.out.println("Parent is creating..");
eat();
}
public void eat(){
System.out.println("Parent is eat: "+eatID);
}
}
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
*Son.java
*/
public class Son extends Parent {
private int eatID=1;
public Son(){
System.out.println("Son is creating..");
eat();
}
public void eat(){
System.out.println("Son is eat: "+eatID);
}
@SuppressWarnings(value={"unused"})
public static void main(String[] args){
Parent p=new Son();
}
}
更多 0 |
|