abstract class Person {
private Clothing clothing;
private String gender;
public Clothing getClothing() {
return clothing;
}
public void setClothing(Clothing clothing){
this.clothing=clothing;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender=gender;
}
public abstract void dress();
}
class Man extends Person {
Man(){
setGender("男人");
}
public void dress() {
Clothing clothing = getClothing();
clothing.personDressCloth(this);
}
}
class Lady extends Person {
Lady(){
setGender("女人");
}
public void dress() {
Clothing clothing = getClothing();
clothing.personDressCloth(this);
}
}
abstract class Clothing {
public abstract void personDressCloth(Person person);
}
class Jacket extends Clothing {
public void personDressCloth(Person person) {
System.out.println(person.getGender()+"穿马甲");
}
}
class Trouser extends Clothing {
public void personDressCloth(Person person) {
System.out.println(person.getGender()+"穿裤子");
}
}
public class TestBridge {
public static void main(String[] args) {
Clothing trouser=new Trouser();
Clothing jacket=new Jacket();
Person man=new Man();
Person lady=new Lady();
man.setClothing(trouser);
man.dress();
man.setClothing(jacket);
man.dress();
lady.setClothing(jacket);
lady.dress();
lady.setClothing(trouser);
lady.dress();
}
}
输出结果:
男人穿裤子
男人穿马甲
女人穿马甲
女人穿裤子