本帖最后由 伍叶竹 于 2014-4-26 13:20 编辑
- class Parent {
- private int flag=0;
- public Parent(){
- System.out.println("Parent is creating..");
- eat();
- }
- public void eat(){
- System.out.println("Parent is eat: "+flag);
- }
-
- }
-
- public class Son extends Parent {
- {System.out.println("i am ok.");}
-
- private int flag=1;
- public Son(){
- System.out.println("Son is creating..");
- eat();
- }
- public void eat(){
- System.out.println("Son is eat: "+flag);
- }
-
- public static void main(String[] args){
- Parent p=new Son();
- }
-
- }
复制代码 打印的结果是:Parent is creating
Son is eat: 0
i am ok.
Son is creating..
Son is eat: 1
大概问题是:第二行打印结果为什么flag的值是0?
|