/**
*
* @author gentleKay
* 使用构造器的步骤 --- 源自于 Java 编程思想
*/
class Meal {
Meal(){
System.out.println("Meal()!"); // ① --- 一
}
}
class Bread {
Bread(){
System.out.println("Bread()!"); //④ --- 四
}
}
class Cheese {
Cheese(){
System.out.println("Cheese()!"); //⑤ --- 五
}
}
class Lettuce {
Lettuce(){
System.out.println("Lettuce()!"); //⑥ --- 六
}
}
class Lunch extends Meal {
//private Bread bb = new Bread();
Lunch(){
System.out.println("Lunch()!"); //② --- 二
}
}
class ProtableLunch extends Lunch {
ProtableLunch(){
System.out.println("ProtableLunch()!"); //③ --- 三
}
}
public class SandWich extends ProtableLunch{
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
public SandWich() {
System.out.println("SandWich()!"); // ⑦ --- 七
}
public static void main(String[] args) {
new SandWich();
}
}
结果:
结论:调用构造器的步骤:
(1)调用基类的构造器。这个步骤会不断的反复递归下去,首先是构造这种层次结构的根,然后是下一层导出类,等等,直至最低层的导出类。
(2)按声明顺序调用成员的初始化方法。
(3)调用导出类构造器的主体。