当多个类之间有继承关系时,创建子类对象会导致父类初始化块的执行。

源代码

public class inherit {

public static void main(String[] args){

first a=new first();

a.show();

}

}

class second{

String name;

{

name="zxr";

}

}

class first extends second{

int age=20;

void show()

{

System.out.println("名字"+name);

System.out.println("年龄"+age);

}

}

原文地址:https://www.cnblogs.com/zxr-1996/p/4888471.html