Java构造时成员初始化的陷阱
让我们先来看两个类:Base和Derived类。注意其中的whenAmISet成员变量,和方法preProcess()
public class Base { Base() { preProcess(); } void preProcess() {} }
public class Derived extends Base { public String whenAmISet = "set when declared"; @Override void preProcess() { whenAmISet = "set in preProcess()"; } }
如果我们构造一个子类实例,那么,whenAmISet 的值会是什么呢?