牛奶报纸...
Code
1using System;
2using System.Collections;
3using System.Text;
4
5namespace TestConsoleApplication
6{
7 public class Class2
8 {
9 public static void Main(string[] args)
10 {
11 NewsPaperSender nsper = new NewsPaperSender();
12 MilkSender milker = new MilkSender();
13
14 Subscriber sb1 = new Subscriber("person1");
15 Subscriber sb2 = new Subscriber("person2");
16 Subscriber sb3 = new Subscriber("person3");
17 Subscriber sb4 = new Subscriber("person4");
18
19
20 nsper.Subscribe(new sendEverthing(sb1.PutMilkIn));
21 nsper.Subscribe(new sendEverthing(sb1.PutMilkIn));
22 milker.Subscribe(new sendEverthing(sb1.PutMilkIn));
23 milker.Subscribe(new sendEverthing(sb4.PutMilkIn));
24 nsper.SendNewsPaper();
25 milker.SendMilk();
26 Console.Read();
27 }
28
29 定义2个接口#region 定义2个接口
30 public interface IMilkBox
31 {
32 void PutMilkIn(string s);
33 }
34
35 public interface INewsPaperBox
36 {
37 void PutNewsPaperIn(string s);
38 }
39 #endregion
40
41 实现类#region 实现类
42 public class Subscriber : INewsPaperBox, IMilkBox
43 {
44 public string name;
45 /**//// <summary>
46 /// 构造函数(牛奶报纸接受者名赋值)
47 /// </summary>
48 /// <param name="name">收到着名字</param>
49 public Subscriber(string name)
50 {
51 this.name = name;
52 }
53 /**//// <summary>
54 /// 实现收到报纸
55 /// </summary>
56 /// <param name="s"></param>
57 public void PutNewsPaperIn(string s)
58 {
59 Console.WriteLine("{0}在NewsPaper箱里收到送来的{1}", name, s);
60 }
61 /**//// <summary>
62 /// 实现收牛奶
63 /// </summary>
64 /// <param name="s"></param>
65 public void PutMilkIn(string s)
66 {
67 Console.WriteLine("{0}在MilkI箱里收到送来的{1}", name, s);
68 }
69 }
70 #endregion
71
72 public delegate void sendEverthing(string s);
73
74 送牛奶和报纸#region 送牛奶和报纸
75 /**//// <summary>
76 /// NewsPaperSender
77 /// </summary>
78 public class NewsPaperSender
79 {
80 public event sendEverthing newsPaperHandler;
81 //protected ArrayList al = new ArrayList();
82
83 public void SendNewsPaper()
84 {
85 //foreach (INewsPaperBox newsPaperBox in al)
86 //{
87 // newsPaperBox.PutNewsPaperIn("NewsPaper");
88 //}
89 if (newsPaperHandler!=null)
90 {
91 newsPaperHandler("NewsPaper");
92 }
93 }
94
95 public void Subscribe(sendEverthing sd)
96 {
97 newsPaperHandler += sd;
98 }
99 }
100 /**//// <summary>
101 /// MilkSender
102 /// </summary>
103 public class MilkSender
104 {
105 //protected ArrayList subscribers = new ArrayList();
106 public event sendEverthing sendMileHandler;
107
108 public void SendMilk()
109 {
110 //foreach (IMilkBox milkBox in subscribers)
111 //{
112 // milkBox.PutMilkIn("MilkI");
113 //}
114 if (sendMileHandler!=null)
115 {
116 sendMileHandler("Milk");
117 }
118 }
119
120 public void Subscribe(sendEverthing sd)
121 {
122 //subscribers.Add(milkBox);
123 sendMileHandler += sd;
124 }
125 }
126 #endregion
127 }
128}
129
1using System;
2using System.Collections;
3using System.Text;
4
5namespace TestConsoleApplication
6{
7 public class Class2
8 {
9 public static void Main(string[] args)
10 {
11 NewsPaperSender nsper = new NewsPaperSender();
12 MilkSender milker = new MilkSender();
13
14 Subscriber sb1 = new Subscriber("person1");
15 Subscriber sb2 = new Subscriber("person2");
16 Subscriber sb3 = new Subscriber("person3");
17 Subscriber sb4 = new Subscriber("person4");
18
19
20 nsper.Subscribe(new sendEverthing(sb1.PutMilkIn));
21 nsper.Subscribe(new sendEverthing(sb1.PutMilkIn));
22 milker.Subscribe(new sendEverthing(sb1.PutMilkIn));
23 milker.Subscribe(new sendEverthing(sb4.PutMilkIn));
24 nsper.SendNewsPaper();
25 milker.SendMilk();
26 Console.Read();
27 }
28
29 定义2个接口#region 定义2个接口
30 public interface IMilkBox
31 {
32 void PutMilkIn(string s);
33 }
34
35 public interface INewsPaperBox
36 {
37 void PutNewsPaperIn(string s);
38 }
39 #endregion
40
41 实现类#region 实现类
42 public class Subscriber : INewsPaperBox, IMilkBox
43 {
44 public string name;
45 /**//// <summary>
46 /// 构造函数(牛奶报纸接受者名赋值)
47 /// </summary>
48 /// <param name="name">收到着名字</param>
49 public Subscriber(string name)
50 {
51 this.name = name;
52 }
53 /**//// <summary>
54 /// 实现收到报纸
55 /// </summary>
56 /// <param name="s"></param>
57 public void PutNewsPaperIn(string s)
58 {
59 Console.WriteLine("{0}在NewsPaper箱里收到送来的{1}", name, s);
60 }
61 /**//// <summary>
62 /// 实现收牛奶
63 /// </summary>
64 /// <param name="s"></param>
65 public void PutMilkIn(string s)
66 {
67 Console.WriteLine("{0}在MilkI箱里收到送来的{1}", name, s);
68 }
69 }
70 #endregion
71
72 public delegate void sendEverthing(string s);
73
74 送牛奶和报纸#region 送牛奶和报纸
75 /**//// <summary>
76 /// NewsPaperSender
77 /// </summary>
78 public class NewsPaperSender
79 {
80 public event sendEverthing newsPaperHandler;
81 //protected ArrayList al = new ArrayList();
82
83 public void SendNewsPaper()
84 {
85 //foreach (INewsPaperBox newsPaperBox in al)
86 //{
87 // newsPaperBox.PutNewsPaperIn("NewsPaper");
88 //}
89 if (newsPaperHandler!=null)
90 {
91 newsPaperHandler("NewsPaper");
92 }
93 }
94
95 public void Subscribe(sendEverthing sd)
96 {
97 newsPaperHandler += sd;
98 }
99 }
100 /**//// <summary>
101 /// MilkSender
102 /// </summary>
103 public class MilkSender
104 {
105 //protected ArrayList subscribers = new ArrayList();
106 public event sendEverthing sendMileHandler;
107
108 public void SendMilk()
109 {
110 //foreach (IMilkBox milkBox in subscribers)
111 //{
112 // milkBox.PutMilkIn("MilkI");
113 //}
114 if (sendMileHandler!=null)
115 {
116 sendMileHandler("Milk");
117 }
118 }
119
120 public void Subscribe(sendEverthing sd)
121 {
122 //subscribers.Add(milkBox);
123 sendMileHandler += sd;
124 }
125 }
126 #endregion
127 }
128}
129
很好的解释了为什么需要委托...