C#委托探索之猫和老鼠

[来源] 达内    [编辑] 达内   [时间]2012-09-07

猫和老鼠:猫来了,老鼠跑掉!怎么实现?

猫和老鼠:猫来了,老鼠跑掉!怎么实现?

< h2 style="margin: 20px 0px 10px; font-size: 20px; background-color: rgb(62, 109, 142); color: white; padding: 2px 5px; font-family: Arial; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">实现代码 < p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 之前实现方式:

< div style="background-color: gray; font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">  1     public  class Cat
 2     {
 3         
 4         public  void ScreamOut(string msg)
 5         {
 6              mouse.RunAway();
 7         }
 8     }
 9     public  class Mouse
10     {
11         
12         public  void RunAway()
13          
14 
15     }
< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 但是如果老鼠繁殖多了,很多老鼠同时都要跑掉,有的向南有的向北,怎么办呢?重写猫的ScreamOut 方法,这样显然不好;

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  现实中的情况是,每个老鼠听到猫来了这个情况,自己有自己的逃跑方式,于是委托用到了这里,即:委托相当于一个方法 但是它的参数也是一个方法,这就不能用一般的方法那样定义了;

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 委托实现如下: 

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 猫类:

< div style="background-color: gray; font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">  1 public  class  Cat

 

 2     {
 3         public  string Name { getset; }
 4         public  delegate void  ScreamEventHandler(object sender, ScreamEventArgs e);
 5         public  event ScreamEventHandler Scream;
 6         public  virtual void OnScream(ScreamEventArgs e)
 7         {
 8              if (this.Scream != null)
 9             {
10                  this .Scream(this, e);
11             }
12         }
13         public  void ScreamOut(string msg)
14         {
15              ScreamEventArgs e = new ScreamEventArgs(msg);
16             OnScream(e);
17         }
18     }
< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 鼠类:

< div style="background-color: gray; font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">  1 public  class  Mouse

 

 2     {
 3         public  string Name { getset; }
 4         public  Mouse(Cat cat)
 5         {
 6              cat.Scream += new Cat.ScreamEventHandler(cat_Scream);
 7              cat.Scream += this.RunAway;
 8         }
 9         public  void RunAway(object sender, ScreamEventArgs e)
10         {
11             Cat c = (Cat)sender;
12              Console.WriteLine(" {0} coming,she said:\"{1}\",{2} running!(Running...) ", c.Name, e.Msg, this.Name);
13         }
14         void  cat_Scream(object sender, ScreamEventArgs e)
15         {
16             Cat c = (Cat)sender;
17              Console.WriteLine(" {0} coming,she said:\"{1}\",{2} running! ", c.Name, e.Msg, this.Name);
18         }
19 
20     }
< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 这里的委托定义和.net Framework 类库定义方式相同,有助于了解系统提供的委托机制,所以还有一个猫叫事件的参数类:

< div style="background-color: gray; font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
复制代码
1 public  class ScreamEventArgs
2     {
3         public  readonly string Msg;
4         public  ScreamEventArgs(string msg)
5         {
6              this.Msg = msg;
7         }

8     } 

复制代码
< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 主类调用代码:

< div style="background-color: gray; font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
复制代码
 1 class Program
 2     {
 3         static  void Main(string[] args)
 4         {
 5              Cat c1 = new Cat();
 6              c1.Name = " tom";
 7 
 8              Mouse m1 = new Mouse(c1);
 9              m1.Name = " Jearry";
10 
11              Mouse m2 = new Mouse(c1);
12              m2.Name = " Jearry's son";
13 
14             c1.Scream += m2.RunAway;
15 
16              c1.ScreamOut(" stop");
17 
18             Console.Read();
19         }

20     } 

复制代码
< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 执行结果:

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">

资源下载