package 继承;
public class Jicheng {
public static void main(String[] args){
Stu s = new Stu();
s.print();
}
}
//father
class Student{
String name;
int age;
{
name="mengxiangtong";
age=21;
}
}
//son
class Stu extends Student{
int grade=99;
void print()
{
System.out.println("name:"+name+"
age:"+age+"
grade:"+grade);
}
}
结果截图:
由此可见当多个类之间有继承关系时,创建子类对象会导致父类初始化块的执行。