慢慢有点感觉了。
在继承类里初始化一个父类,也就是基本数据类型的延伸吧。
那进一步想,在多线程环境下,信号同步和锁,共享内存的实现不是又增加了现实难度?
1 /* 2 * Created by SharpDevelop. 3 * User: home 4 * Date: 2013/4/21 5 * Time: 9:33 6 * 7 * To change this template use Tools | Options | Coding | Edit Standard Headers. 8 */ 9 using System; 10 11 namespace Decorator 12 { 13 class Program 14 { 15 public static void Main(string[] args) 16 { 17 Person xc = new Person("小菜"); 18 19 Console.WriteLine("\n 第一种装扮: "); 20 21 BigTrouser kk = new BigTrouser(); 22 Tshirts dtx = new Tshirts(); 23 24 kk.Decorate(xc); 25 dtx.Decorate(kk); 26 dtx.Show(); 27 28 Console.Write("Press any key to continue . . . "); 29 Console.ReadKey(true); 30 } 31 } 32 class Person 33 { 34 public Person() 35 { } 36 private string name; 37 public Person(string name) 38 { 39 this.name = name; 40 } 41 public virtual void Show() 42 { 43 Console.WriteLine("装扮的 {0}", name); 44 } 45 } 46 class Finery : Person 47 { 48 protected Person component; 49 50 public void Decorate(Person component) 51 { 52 this.component = component; 53 } 54 55 public override void Show() 56 { 57 if (component != null) 58 { 59 component.Show(); 60 } 61 } 62 } 63 64 class Tshirts : Finery 65 { 66 public override void Show() 67 { 68 Console.WriteLine("大T恤"); 69 base.Show(); 70 } 71 } 72 73 class BigTrouser : Finery 74 { 75 public override void Show() 76 { 77 Console.WriteLine("大垮裤"); 78 base.Show(); 79 } 80 } 81 }
时间: 2024-11-01 21:19:28