Java 子类 和 父类 执行顺序

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

 

如果 父类和子类 构造方法没有对应,则直接编译错误,直接报错

posted on 2021-04-12 12:35  正义的伙伴!  阅读(186)  评论(0编辑  收藏  举报

导航

//增加一段JS脚本,为目录生成使用