public class Test184 extends c184 {
private a184 at = new a184("a in t");
private int i = f(); Test184() {
System.out.println("this is test184");
} public int f() {
System.out.println("this is t f()");
return 1;
} public static void main(String[] args) {
Test184 t = new Test184();
}}class a184 {
a184(String s) {
System.out.println("this is a.." + s);
}
}class b184 {
private a184 ab = new a184("a in b"); b184() {
System.out.println("this is b..");
}
}class c184 extends b184 {
private a184 ac = new a184("a in c"); c184() {
System.out.println("this is c..");
}
}
运行结果:
this is a..a in b
this is b..
this is a..a in c
this is c..
this is a..a in t  
this is t f()
this is test184请问下红色部分为什么不先被初始化呢?

解决方案 »

  1.   

    你先从main()为入口
    要实例化Test184需实例化c184,实例化c184需要实例化b184
    实例化b184需先要初始化实例变量ab
    class b184
    {    b184()
        {
            ab = new a184("a in b");
            System.out.println("this is b..");
        }    private a184 ab;
    }
    b184实例化后再实例化c184
    同理需要初始化实例变量ac
    class c184 extends b184
    {    c184()
        {
            ac = new a184("a in c");
            System.out.println("this is c..");
        }    private a184 ac;
    }
    最后实例化Test184初始化实例变量
    at,i
    public class Test184 extends c184
    {    Test184()
        {
            at = new a184("a in t");
            i = f();
            System.out.println("this is test184");
        }    public int f()
        {
            System.out.println("this is t f()");
            return 1;
        }    public static void main(String args[])
        {
            Test184 test184 = new Test184();
        }    private a184 at;
        private int i;
    }
    明白不明白其中的代码是我用反编译工具反编译的.不理解还可以再问
      

  2.   

    public class Test184 extends c184 {系统先递归初始化父类
     
      

  3.   

    我的意思是Test184类先被初始化,其类中的变量应该是在其构造器之前被初始化吧,那是不是应该先初始化类a184?
    是不是我这样理解错的?
      

  4.   

    在类Test184中的变量at,i 是不是应该在Test184的构造器被初始化之前被先初始化?
      

  5.   


    public class Test184 extends c184 {
    private a184 at = new a184("a in t");
    private int i = f(); Test184() {
    a184 at1 = new a184("a in t..Test184()");
    System.out.println("this is test184");
    } public int f() {
    System.out.println("this is t f()");
    return 1;
    } public static void main(String[] args) {
    Test184 t = new Test184();
    }}class a184 {
    a184(String s) {
    System.out.println("this is a.." + s);
    }
    }class b184 {
    private a184 ab = new a184("a in b"); b184() {
    System.out.println("this is b..");
    }
    }class c184 extends b184 {
    private a184 ac = new a184("a in c"); c184() {
    System.out.println("this is c..");
    }
    }
    那我要是在Test184类构造器中再放个变量呢?他是什么时候被初始化?
    谢谢大哥们了~~
      

  6.   

    this is a..a in b 
    this is b.. 
    this is a..a in c 
    this is c.. 
    this is a..a in t  
    this is t f() 
    this is a..a in t..Test184()
    this is test184 
      

  7.   

    因为test184是继承于 c184,生成对象时是先要初始化基类的,而c184又继承于b184,所以又要先初始化b184,b184已经是最上层的基类了,所以先初始化b184中的private a184类型的变量ab ,因为变量是a184类型,所以先调用a184的构造函数,this is a..a in b 打印,接着再递归回去!
      

  8.   

     main函数是入口,然后依次将调用的那些类递归初始,也就是12楼说的顺序进行初始化,就得出正解了