标签:div 实施 main 理解 pre bsp rdd 情况 out
Person类
1 package thirdDecoration; 2 3 // 相当于ConcreteCompement的person 4 public class Person { 5 private String name; 6 public Person(){} 7 public Person(String name){ 8 this.name=name; 9 } 10 11 public void show(){ //由于在Java中,普通类不能拥有抽象方法,且抽象方法无方法体,所以和书上C#的写法不同 12 System.out.println("装扮的"+this.name); 13 }; 14 }
服饰类
1 package thirdDecoration; 2 3 public class Finery extends Person { 4 protected Person compenent; 5 public void Decorate(Person person){ 6 this.compenent=person; 7 } 8 public void show(){ //通过覆写的方式增加子类的特性 9 if (compenent!=null){ 10 compenent.show(); 11 } 12 } 13 }
具体服饰类
1 package thirdDecoration; 2 3 public class Tshirt extends Finery { 4 public void show(){ 5 System.out.println("T恤"); 6 super.show(); 7 } 8 }
1 package thirdDecoration; 2 3 public class Skirt extends Finery { 4 public void show(){ 5 System.out.println("超短裙"); 6 super.show(); 7 } 8 }
1 package thirdDecoration; 2 3 public class Dress extends Finery { 4 public void show(){ 5 System.out.println("连衣裙"); 6 super.show(); 7 } 8 }
客户端
1 package thirdDecoration; 2 3 public class DecorationTest { 4 5 public static void main(String[] args) { 6 Person person=new Person("小仙猪");//把对象找出来 7 Tshirt tshirt=new Tshirt(); //把衣服准备好 8 Skirt skirt=new Skirt(); //把衣服准备好 9 10 skirt.Decorate(person);//裙子穿上 11 tshirt.Decorate(skirt);//再把上衣穿上 12 tshirt.show(); 13 14 System.out.println("done!"); 15 } 16 17 }
分析:
标签:div 实施 main 理解 pre bsp rdd 情况 out
原文地址:https://www.cnblogs.com/zuolanlan/p/10004030.html