考察知识点
- 什么是初始块
- 类实例化的执行过程(包括初始块)
- 继承、覆盖的理解
代码
public class Demo{
static class Parent {
static {
System.out.println("A");
}
{
System.out.println("B");
}
static{
System.out.println("C");
}
public Parent() {
System.out.println("construct Parent.");
}
public void hello(){
System.out.println("Parent.hello");
}
}
static class Child extends Parent{
static {
System.out.println("a");
}
{
System.out.println("b");
}
static{
System.out.println("c");
}
public Child() {
System.out.println("construct Child");
}
public void hello(){
System.out.println("Child.hello");
}
}
public static void main(String[] args) {
Object child = new Child();
Parent pp = (Parent) child;
pp.hello();
}
}
输出
A
C
a
c
B
construct Parent.
b
construct Child
Child.hello