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