1 using System; 2 3 namespace ConsoleApplication2 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 //先造一部手机 10 SJ sj = new SjLoad(); 11 //再给手机贴膜 12 SJ sjm = new SJM(sj); 13 Console.WriteLine(sjm.Getsj()); 14 //加了挂饰的手机 15 SJ gs = new GS(sj); 16 Console.WriteLine(gs.Getsj()); 17 //贴了膜又加挂饰手机 18 SJ gsm = new GS(sjm); 19 Console.WriteLine(gsm.Getsj()); 20 21 //造人 22 SJ ren = new Ren(); 23 //给枪 24 SJ sq = new SQ(ren); 25 //给容貌 26 SJ rm = new RM(sq); 27 Console.WriteLine(rm.Getsj()); 28 Console.Read(); 29 } 30 } 31 32 //建造厂 33 public abstract class SJ 34 { 35 //建造功能 36 public abstract string Getsj(); 37 } 38 39 //先造一部手机 40 public class SjLoad:SJ{ 41 public override string Getsj() 42 { 43 return "手机"; 44 } 45 } 46 47 //给建造厂配备装饰功能 48 public abstract class ZS:SJ 49 { 50 private SJ sj; //准备装手机的容器 51 public ZS(SJ s) //拿一个手机过来 52 { 53 this.sj = s; //把手机装进容器 54 } 55 56 //经过装饰的手机 57 public override string Getsj() 58 { 59 return sj.Getsj(); 60 } 61 } 62 63 //给手机贴膜 64 public class SJM : ZS 65 { 66 public SJM(SJ sj) //拿过来一个手机 67 :base(sj) 68 { 69 } 70 //给手机贴膜 71 public override string Getsj() 72 { 73 return "贴了膜的"+base.Getsj(); 74 } 75 } 76 77 //加挂饰 78 public class GS : ZS 79 { 80 public GS(SJ sj):base(sj) { } 81 public override string Getsj() 82 { 83 return "+挂饰"+base.Getsj(); 84 } 85 } 86 87 //造个人 88 public class Ren : SJ 89 { 90 public override string Getsj() 91 { 92 return "人"; 93 } 94 } 95 96 //给人配一把手枪 97 public class SQ : ZS 98 { 99 public SQ(SJ s) : base(s) { } //把人抓过来 100 private string dsq = "拿着手枪的"; 101 public override string Getsj() //配手枪 102 { 103 return dsq+ base.Getsj(); 104 } 105 } 106 107 //给人容貌 108 public class RM : ZS 109 { 110 public RM(SJ s) : base(s) { } 111 112 private string rmao = "英俊的"; 113 public override string Getsj() 114 { 115 return rmao+ base.Getsj(); 116 } 117 } 118 }
时间: 2024-11-07 21:26:18