key point
子类构造函数
public class Father {
public void draw() {
System.out.println("father draw");
}
public Father() {
System.out.println("father:before draw");
draw();
System.out.println("father:after draw");
}
}
public class Son extends Father {
private int number = 1;
public Son(int number) {
this.number = number;
System.out.println("init number:" + this.number);
}
@Override
public void draw() {
System.out.println("son draw number:" + this.number);
}
}
public class MainTest <T> extends Object{
public static void main(String[] args) {
new Son(5);
}
}
输出结果
father:before draw
son draw number:0
father:after draw
init number:5
子类构造函数的第一行都是super(); 如果没有super()的话 编译器自动增加super.
基类的构造函数调用draw. 因为virtureinvoke 的关系 会调用子类的draw
子类的number 没有被初始化完成. 只有默认的0
完成基类的构造, 回到子类
完成构造