刚换了下家,没想到刚工作了一周就有一个测试,真是触不及防。通过做题让我看到了基础的不足。特在此将此题做一记录。
题面是:一个父类有一个静态代码块、一个构造代码块、一个构造函数,然后就是一个子类继承这个父类,也包含这三个方法,请写出他们的执行顺序。
下面先用代码演示一下:
public class Father {
static {
System.out.println("the father static");
name = "in father static name";
}
public static String name = "father";
{
System.out.println("the father blocks");
}
public Father(){
System.out.println("the father constructor");
}
}
public class Son extends Father {
public static String name = "son";
static {
System.out.println("the son static");
name = "in son static name and before";
}
{
System.out.println("the son blocks");
}
public Son(){
System.out.println("the son constructor");
}
public static void main(String[] args) {