public class Parent { static { System.out.println("parent static"); } { System.out.println("parent code block"); } public Parent(String name){ System.out.println("parent init"); } }
public class Child extends Parent { static { System.out.println("child static"); } { System.out.println("child code block"); } public Child(){ System.out.println("child init"); } public void echo(){ System.out.println("this is child"); } public static void main(String[] args) { new Child().echo(); System.out.println("==============="); new Child().echo(); } }
执行结果:
parent static child static parent code block parent init child code block child init this is child =============== parent code block parent init child code block child init this is child
如果 父类和子类 构造方法没有对应,则直接编译错误,直接报错