我有两节课,父母和孩子
public class Parent {
public Parent() {
System.out.println("Parent Constructor");
}
static {
System.out.println("Parent static block");
}
{
System.out.println("Parent initialisation block");
}
}
public class Child extends Parent {
{
System.out.println("Child initialisation block");
}
static {
System.out.println("Child static block");
}
public Child() {
System.out.println("Child Constructor");
}
public static void main(String[] args) {
new Child();
}
}
以上代码的输出将为
Parent static block
Child static block
Parent initialization block
Parent Constructor
Child initialization block
Child Constructor
为什么 Java 以这种顺序执行代码? 决定执行顺序的规则是什么?