现在有一个抽象类 GenericWithCreate
abstract class GenericWithCreate<T> {
final T element;
GenericWithCreate() {
System.out.println("GenericWithCreate constructor");
element = create();
}
abstract T create();
}
继承该抽象类
public class Creator extends GenericWithCreate<X> {
public Creator() {
System.out.println("Creator constructor");
}
@Override
X create() {
System.out.println("Creator create()");
return new X();
}
void f() {
System.out.println(element.getClass().getSimpleName());
}
}
可见,在抽象基类中调用了抽象方法,那么这时候构造方法等执行流程是什么样呢?
public class CreatorGeneric {
public static void main(String[] args) {
Creator c = new Creator();
c.f();
}
}
打印输出
GenericWithCreate constructor
Creator create()
Creator constructor
X
可见,抽象基类构造方法调用后,会调用子类的抽象方法,然后再调用子类的构造方法。