1 /** 2 * 人 3 * @author Persion 4 * 5 */ 6 public class Persion { 7 8 private String name; 9 10 public Persion(){} 11 12 public Persion(String name){ 13 14 this.name = name; 15 } 16 17 18 public void show(){ 19 System.err.print("装扮:"+name+" "); 20 } 21 22 }
Persion.java
1 public class Finery extends Persion{ 2 3 protected Persion persion; 4 5 public void Decorate(Persion persion){ 6 this.persion=persion; 7 } 8 9 public void show(){ 10 if(persion != null){ 11 persion.show(); 12 } 13 } 14 15 }
Finery.java
1 public class BigTrouser extends Finery { 2 3 @Override 4 public void show(){ 5 System.out.print("裤子 "); 6 persion.show(); 7 } 8 }
BigTrouser.java
1 public class TShrts extends Finery{ 2 3 @Override 4 public void show(){ 5 System.out.print("大T恤 "); 6 persion.show(); 7 } 8 9 }
TShrts.java
1 /** 2 * 装饰模式 3 * @author lucksheep 4 * 5 */ 6 public class Test { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 13 Persion persion=new Persion("小肥羊"); 14 15 TShrts shrts=new TShrts(); 16 BigTrouser bt=new BigTrouser(); 17 18 shrts.Decorate(persion); 19 bt.Decorate(shrts); 20 21 bt.show(); 22 } 23 24 }
Test.java
呼呼 有错的地方望指点,最近再看这些基础的东西,稳打稳进
时间: 2024-10-12 04:49:06