昨儿做到一笔试题,考父类子类初始化的问题,做完觉得有点心虚,就回来实验了一下:
输出:
public class FCTest {
public static void main(String[] args){
Child c = new Child();
c.v();
Father f = new Father();
f.v();
}
}
class Father{
static {
System.out.println("Father static");
}
public Father(){
System.out.println("Father()");
}
public void f(){
System.out.println("Father f()");
}
public void v(){
f();
}
}
class Child extends Father{
static {
System.out.println("Child static");
}
public Child(){
System.out.println("Child()");
}
public void f(){
System.out.println("Child f()");
}
}
输出:
Father static
Child static
Father()
Child()
Child f()
Father()
Father f()