牛奶报纸...

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Collections;
  3using System.Text;
  4
  5namespace TestConsoleApplication
  6ExpandedBlockStart.gifContractedBlock.gif{
  7    public class Class2
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  9        public static void Main(string[] args)
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 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
 29ContractedSubBlock.gifExpandedSubBlockStart.gif        定义2个接口#region 定义2个接口
 30        public interface IMilkBox
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 32            void PutMilkIn(string s);
 33        }

 34
 35        public interface INewsPaperBox
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 37            void PutNewsPaperIn(string s);
 38        }

 39        #endregion

 40
 41ContractedSubBlock.gifExpandedSubBlockStart.gif        实现类#region 实现类
 42        public class Subscriber : INewsPaperBox, IMilkBox
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 44            public string name;
 45ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 46            /// 构造函数(牛奶报纸接受者名赋值)
 47            /// </summary>
 48            /// <param name="name">收到着名字</param>

 49            public Subscriber(string name)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 51                this.name = name;
 52            }

 53ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 54            /// 实现收到报纸
 55            /// </summary>
 56            /// <param name="s"></param>

 57            public void PutNewsPaperIn(string s)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 59                Console.WriteLine("{0}在NewsPaper箱里收到送来的{1}", name, s);
 60            }

 61ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 62            /// 实现收牛奶
 63            /// </summary>
 64            /// <param name="s"></param>

 65            public void PutMilkIn(string s)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 67                Console.WriteLine("{0}在MilkI箱里收到送来的{1}", name, s);
 68            }

 69        }

 70        #endregion

 71
 72        public delegate void sendEverthing(string s);
 73
 74ContractedSubBlock.gifExpandedSubBlockStart.gif        送牛奶和报纸#region 送牛奶和报纸
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 76        /// NewsPaperSender
 77        /// </summary>

 78        public class NewsPaperSender
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 80            public event sendEverthing newsPaperHandler;
 81            //protected ArrayList al = new ArrayList();
 82
 83            public void SendNewsPaper()
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 85                //foreach (INewsPaperBox newsPaperBox in al)
 86                //{
 87                //    newsPaperBox.PutNewsPaperIn("NewsPaper");
 88                //}
 89                if (newsPaperHandler!=null)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 91                    newsPaperHandler("NewsPaper");
 92                }

 93            }

 94
 95            public void Subscribe(sendEverthing sd)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 97                newsPaperHandler += sd;
 98            }

 99        }

100ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
101        /// MilkSender
102        /// </summary>

103        public class MilkSender
104ExpandedSubBlockStart.gifContractedSubBlock.gif        {
105            //protected ArrayList subscribers = new ArrayList();
106            public event sendEverthing sendMileHandler;
107
108            public void SendMilk()
109ExpandedSubBlockStart.gifContractedSubBlock.gif            {
110                //foreach (IMilkBox milkBox in subscribers)
111                //{
112                //    milkBox.PutMilkIn("MilkI");
113                //}
114                if (sendMileHandler!=null)
115ExpandedSubBlockStart.gifContractedSubBlock.gif                {
116                    sendMileHandler("Milk");
117                }

118            }

119
120            public void Subscribe(sendEverthing sd)
121ExpandedSubBlockStart.gifContractedSubBlock.gif            {
122                //subscribers.Add(milkBox);
123                sendMileHandler += sd;
124            }

125        }

126        #endregion

127    }

128}

129

很好的解释了为什么需要委托...

转自: http://www.cnblogs.com/zhenyulu/articles/76706.html

转载于:https://www.cnblogs.com/whoseyourlady/archive/2009/04/29/1446444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值