事件之特别委托之处


事件是委托的特殊形式。特殊在什么地方呢:事件必须多播。多播必须使用+=而不能用=加载。

事件本身是 Observer 模式“推”模型的标准表现形式。第一个参数object Sender是事件的监测对象,用“this ”的话就是触发事件的类,可以使"观察者"访问到触发事件的类的内部成员变量。第二个参数EventArgs是你要推给"观察者"的数据,当然你要用一个继承自EventArgs的类把这些数据封装好。

示例代码如下:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace  Delegate  dot.gif {
InBlock.gif    
// 热水器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public class Heater dot.gif{
InBlock.gif       
private int temperature;
InBlock.gif       
public string type = "RealFire 001";       // 添加型号作为演示
InBlock.gif
       public string area = "China Xian";         // 添加产地作为演示
InBlock.gif       
//声明委托
InBlock.gif
       public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e);
InBlock.gif       
public event BoiledEventHandler Boiled; //声明事件
InBlock.gif
InBlock.gif       
// 定义BoiledEventArgs类,传递给Observer所感兴趣的信息
ExpandedSubBlockStart.gifContractedSubBlock.gif
       public class BoiledEventArgs : EventArgs dot.gif{
InBlock.gif           
public readonly int temperature;
ExpandedSubBlockStart.gifContractedSubBlock.gif           
public BoiledEventArgs(int temperature) dot.gif{
InBlock.gif              
this.temperature = temperature;
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif       }

InBlock.gif
InBlock.gif       
// 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视
ExpandedSubBlockStart.gifContractedSubBlock.gif
       protected virtual void OnBoiled(BoiledEventArgs e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
if (Boiled != nulldot.gif// 如果有对象注册
InBlock.gif
              Boiled(this, e);  // 调用所有注册对象的方法
ExpandedSubBlockEnd.gif
           }

ExpandedSubBlockEnd.gif       }

InBlock.gif       
InBlock.gif       
// 烧水。
ExpandedSubBlockStart.gifContractedSubBlock.gif
       public void BoilWater() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
for (int i = 0; i <= 100; i++dot.gif{
InBlock.gif              temperature 
= i;
ExpandedSubBlockStart.gifContractedSubBlock.gif              
if (temperature > 95dot.gif{
InBlock.gif                  
//建立BoiledEventArgs 对象。
InBlock.gif
                  BoiledEventArgs e = new BoiledEventArgs(temperature);
InBlock.gif                  OnBoiled(e);  
// 调用 OnBolied方法
ExpandedSubBlockEnd.gif
              }

ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 警报器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public class Alarm dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public void MakeAlert(Object sender, Heater.BoiledEventArgs e) dot.gif{
InBlock.gif           Heater heater 
= (Heater)sender;     //这里是不是很熟悉呢?
InBlock.gif           
//访问 sender 中的公共字段
InBlock.gif
           Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type);
InBlock.gif           Console.WriteLine(
"Alarm: 嘀嘀嘀,水已经 {0} 度了:", e.temperature);
InBlock.gif           Console.WriteLine();
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 显示器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public class Display dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public static void ShowMsg(Object sender, Heater.BoiledEventArgs e) dot.gif{   //静态方法
InBlock.gif
           Heater heater = (Heater)sender;
InBlock.gif           Console.WriteLine(
"Display:{0} - {1}: ", heater.area, heater.type);
InBlock.gif           Console.WriteLine(
"Display:水快烧开了,当前温度:{0}度。", e.temperature);
InBlock.gif           Console.WriteLine();
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
class Program dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
static void Main() dot.gif{
InBlock.gif           Heater heater 
= new Heater();
InBlock.gif           Alarm alarm 
= new Alarm();
InBlock.gif
InBlock.gif           heater.Boiled 
+= alarm.MakeAlert;   //注册方法
InBlock.gif
           heater.Boiled += (new Alarm()).MakeAlert;      //给匿名对象注册方法
InBlock.gif
           heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert);    //也可以这么注册
InBlock.gif
           heater.Boiled += Display.ShowMsg;       //注册静态方法
InBlock.gif

InBlock.gif           heater.BoilWater();   
//烧水,会自动调用注册过对象的方法
ExpandedSubBlockEnd.gif
       }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif输出为:
None.gifAlarm:China Xian 
-  RealFire  001 :
None.gifAlarm: 嘀嘀嘀,水已经 
96  度了:
None.gifAlarm:China Xian 
-  RealFire  001 :
None.gifAlarm: 嘀嘀嘀,水已经 
96  度了:
None.gifAlarm:China Xian 
-  RealFire  001 :
None.gifAlarm: 嘀嘀嘀,水已经 
96  度了:
None.gifDisplay:China Xian 
-  RealFire  001 :
None.gifDisplay:水快烧开了,当前温度:96度。
None.gif
//  省略 dot.gif



比较

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace  Delegate  dot.gif {
InBlock.gif    
// 热水器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public class Heater dot.gif{
InBlock.gif       
private int temperature;
InBlock.gif       
public delegate void BoilHandler(int param);   //声明委托
InBlock.gif
       public event BoilHandler BoilEvent;        //声明事件
InBlock.gif
InBlock.gif       
// 烧水
ExpandedSubBlockStart.gifContractedSubBlock.gif
       public void BoilWater() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
for (int i = 0; i <= 100; i++dot.gif{
InBlock.gif              temperature 
= i;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
if (temperature > 95dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
if (BoilEvent != nulldot.gif//如果有对象注册
InBlock.gif
                      BoilEvent(temperature);  //调用所有注册对象的方法
ExpandedSubBlockEnd.gif
                  }

ExpandedSubBlockEnd.gif              }

ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 警报器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public class Alarm dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public void MakeAlert(int param) dot.gif{
InBlock.gif           Console.WriteLine(
"Alarm:嘀嘀嘀,水已经 {0} 度了:", param);
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 显示器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public class Display dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public static void ShowMsg(int param) dot.gif//静态方法
InBlock.gif
           Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", param);
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
class Program dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
static void Main() dot.gif{
InBlock.gif           Heater heater 
= new Heater();
InBlock.gif           Alarm alarm 
= new Alarm();
InBlock.gif
InBlock.gif           heater.BoilEvent 
+= alarm.MakeAlert;    //注册方法
InBlock.gif
           heater.BoilEvent += (new Alarm()).MakeAlert;   //给匿名对象注册方法
InBlock.gif
           heater.BoilEvent += Display.ShowMsg;       //注册静态方法
InBlock.gif

InBlock.gif           heater.BoilWater();   
//烧水,会自动调用注册过对象的方法
ExpandedSubBlockEnd.gif
       }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif输出为:
None.gifAlarm:嘀嘀嘀,水已经 
96  度了:
None.gifAlarm:嘀嘀嘀,水已经 
96  度了:
None.gifDisplay:水快烧开了,当前温度:96度。
None.gif

转载于:https://www.cnblogs.com/waitrabbit/archive/2008/05/20/1203621.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值