1. 程式人生 > >繼承父類的靜態方法的加載順序

繼承父類的靜態方法的加載順序

xtend 加載順序 oid span 結果 靜態方法 div 父類 main

public class GrandFather {

    static{
        System.out.println("GrandFather在靜態代碼塊");
    }
}

public class Father extends GrandFather{ static{ System.out.println("Father在靜態代碼塊"); } public static int fatherAge = 55; public Father(){ System.out.println(
"我是Father~"); }
}


public class Son extends Father{ static { System.out.println("Son在靜態代碼塊"); } public static int sonAge = 25; public Son(){ System.out.println("我是Son~"); } }

測試:

public class FGSTest{

    public static void main(String[] args) {

        System.out.println(
"main歲數:" + Son.fatherAge); //入口 1 // System.out.println("main歲數:" + Son.sonAge); //入口 2 } }

結果:

GrandFather在靜態代碼塊
Father在靜態代碼塊
main歲數:55

public class FGSTest{

    public static void main(String[] args) {

        //System.out.println("main歲數:" + Son.fatherAge); //入口  1
        System.out.println("main歲數:" + Son.sonAge); //
入口 2 } }

結果:

GrandFather在靜態代碼塊
Father在靜態代碼塊
Son在靜態代碼塊
main歲數:25

繼承父類的靜態方法的加載順序