【26】java的组合与继承

public class Plate {

public Plate(int i){
System.out.println("Plate constructor");
}
}
public class DinnerPlate extends Plate {

public DinnerPlate(int i){
super(i);
System.out.println("DinnerPlage constructor");
}
}
public class Utensil {

public Utensil(int i){
System.out.println("Utensil constructor");
}
}
public class Spoon extends Utensil {

public Spoon(int i){
super(i);
System.out.println("Spoon constructor");
}
}
public class Fork extends Utensil {

public Fork(int i){
super(i);
System.out.println("Fork constructor");
}
}
public class Knife extends Utensil {

public Knife(int i){
super(i);
System.out.println("Knife constructor");
}
}
public class Custom {

public Custom(int i){
System.out.println("Custom constructor");
}
}
public class PlaceSetting extends Custom {//继承

Spoon spoon;//组合
Knife knife;//组合
Fork fork;//组合
DinnerPlate dPlate;//组合

public PlaceSetting(int i){
super(i + 2);//先初始化父类

spoon = new Spoon(i + 3);//再初始化组合类
knife = new Knife(i + 4);//再初始化组合类
fork = new Fork(i + 5);//再初始化组合类
dPlate = new DinnerPlate(i + 6);//再初始化组合类

System.out.println("PlaceSetting constructor");
}

public static void main(String args[]){
new PlaceSetting(666);
}
}
//总之,先初始化父类,再初始化组合类,如果组合类同样有父类,也会先初始化,最后初始化自己









posted @ 2012-03-21 13:52  Marstar  阅读(3174)  评论(0编辑  收藏  举报