待装饰的主类
public class Person {
public Person() {
}
private String name;
public Person(String name) {
this.name = name;
}
public void show(){
System.out.println("装饰的"+name);
}
}
服饰类
public class Finery extends Person{
protected Person component;
public void setComponent(Person component){
this.component=component;
}
public void show(){
if(component!=null){
component.show();
}
}
}
实现服饰方法的子类
public class LeatherShoes extends Finery{
public void show(){
System.out.println("皮鞋 ");
super.show();
}
}
public class BigTrouser extends Finery{
public void show(){
System.out.println("垮裤 ");
super.show();
}
}
public class Sneakers extends Finery{
public void show(){
System.out.println("破球鞋 ");
super.show();
}
}
public class Suit extends Finery{
public void show(){
System.out.println("西装 ");
super.show();
}
}
public class Tie extends Finery{
public void show(){
System.out.println("领带 ");
super.show();
}
}
public class TShirts extends Finery{
public void show(){
System.out.println("大T恤 ");
super.show();
}
}
主测试类
public class Program {
public static void main(String[] args){
Person xc=new Person("小菜");
System.out.println("第一种装扮");
Sneakers sneakers=new Sneakers();
BigTrouser bigTrouser=new BigTrouser();
TShirts tShirts=new TShirts();
sneakers.setComponent(xc);
bigTrouser.setComponent(sneakers);
tShirts.setComponent(bigTrouser);
tShirts.show();
}
}