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

 

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

相关文章:

  • 2021-08-02
  • 2021-07-31
  • 2021-09-14
  • 2018-11-03
  • 2021-08-11
  • 2020-12-20
  • 2021-09-25
  • 2021-10-01
猜你喜欢
  • 2021-08-27
  • 2018-10-10
  • 2021-07-27
  • 2021-05-25
  • 2018-10-30
  • 2021-11-06
  • 2021-12-23
  • 2018-05-24
相关资源
相似解决方案