一个Java小程序,帮助理解Java继承中的初始化过程

class Insec{
    private int i = 9;
    protected int j;
    Insec(){
        System.out.println("i = " + i + ", " + "j = " + j);
        j = 39;
    }
    private static int x1 = printInt("static Insec.x1 init");
    static int printInt(String s){
        System.out.println(s);
        return 47;
    }
    private int k = printInt("Insec init k");
}
public class Garbage extends Insec{
 
    private int k = printInt("beetle.k init");
 
    public Garbage(){
        System.out.println("k = " + k);
        System.out.println("j = " + j);
    }
    public int q = printInt("beetle.q init");
    private static int x2 = printInt("static beetle.x2 init");
     
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("beetle construct");
        Garbage b = new Garbage();
    }
}

输出:

static Insec.x1 init
static beetle.x2 init
beetle construct
Insec init k
i = 9, j = 0
beetle.k init
beetle.q init
k = 47
j = 39

  

 

posted @ 2013-04-06 19:37  E_star  阅读(491)  评论(0编辑  收藏  举报