1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class Parent { private static final String PARENT = "parent"; private static final String CHILD = "child"; public Parent() { System.out.print(PARENT + "A "); } static { System.out.print(PARENT + "B "); } { System.out.print(PARENT + "C "); } static class Child extends Parent { public Child() { System.out.print(CHILD + "A "); } static { System.out.print(CHILD + "B "); } { System.out.print(CHILD + "C "); } } public static void main(String[] args) { Child child = new Child(); } }
|
输出结果:parentB childB parentC parentA childC childA
官方解析:考察父类、子类的加载顺序:
父类的静态代码块 > 子类的静态代码块 > 父类的动态代码块 > 父类的构造方法 > 子类的动态代码块 > 子类的构造方法。
2、
3、