委托与事件

委托是对函数的封装,可以当作给方法的特征指定一个名称。而事件则是委托的一种特殊形式。当发生有意义的事情时,事件对象处理通知过程。事件其实就是设计模式中观察者模式在.NET中的一种实现方式。

委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托对象用关键字delegate来声明。

事件是说在发生其他类或对象关注的事情时,类或对象可通过事件通知它们。事件对象用event关键字声明。

 1 class Cat
 2 {
 3   private string name;
 4   public Cat(string name)
 5   {
 6     this.name = name;
 7   }
 8   // 声明委托
 9   public delegate void CatShoutEventHandler();
10 
11   // 声明事件 CatShout,它的事件类型是委托 CatShoutEventHandler
12   public event CatShoutEventHandler CatShout;
13 
14   public void Shout()
15   {
16     Consoel.WriteLine(" ");
17 
18     // 如果 CatShout 中有对象登记事件,则执行 CatShout()
19     if(CatShout != null)   CatShout();
20   }
21 }
22 
23 class Mouse
24 {
25   private string name;
26   public Cat(string name)
27   {
28     this.name = name;
29   }
30 
31   public void Run()
32   {
33     Console.WriteLine(" ");
34   }
35 }
36 
37 static void Main(string[] args)
38 {
39   Cat cat = new Cat("Tom");
40   Mouse mouse1 = new Mouse("Jerry");
41   Mouse mouse2 = new Mouse("Jack");
42 
43   // 表示将 Mouse 的 Run 方法通过实例化委托 Cat.CatShoutEventHandler 登记到 Cat 的事件 CatShout 当中。
44   // 其中 “+=” 表示 “add_CatShout” 的意思。
45   cat.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);
46   cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);
47 
48   cat.Shout();
49 }

【注】

new Cat.CatShoutEventHandler(mouse1.Run) 的含义是实例化一个委托,而委托的实例其实就是Mouse 的 Run 方法。而 “cat.CatShout +=” 表示的就是 “cat.add_CatShout(new Cat.CatShoutEventHandler(mouse1.Run))”的意思。

“+=” :增加委托实例对象,add_CatShout()。
“-=” :移除委托实例对象,remove_CatShout()。

EventArgs 是包含事件数据的类的基类。换句话说,这个类的作用就是用来在事件触发时传递数据用的。object sender 就是传递发送通知的对象, 而EventArgs 是包含事件的类。

 1 public class CatShoutEventArgs : EventArgs
 2 {
 3   private string name;
 4   public string Name
 5   {
 6     get { return name; }
 7     set { name = value; }
 8   }
 9 }
10 
11 class Cat
12 {
13   private string name;
14   public Cat(string name)
15   {
16     this.name = name;
17   }
18   // 声明委托
19   public delegate void CatShoutEventHandler(object sender, CatShoutEventArgs args);
20 
21   // 声明事件 CatShout,它的事件类型是委托 CatShoutEventHandler
22   public event CatShoutEventHandler CatShout;
23 
24   public void Shout()
25   {
26     Consoel.WriteLine("我是{0} ", name);
27 
28     // 如果 CatShout 中有对象登记事件,则执行 CatShout()
29     if(CatShout != null)   
30     {
31       // 声明并实例化一个 CatShoutEventArgs,并给 Name 属性赋值为猫的名字
32       CatShoutEventArgs e = new CatShoutEventArgs();
33       e.Name = this.name;
34 
35       // 当事件触发时,通知所有登记过的对象,并将发送通知的自己以及需要的数据传递过去
36       CatShout(this, e);
37     }
38   }
39 }
40 
41 class Mouse
42 {
43   private string name;
44   public Cat(string name)
45   {
46     this.name = name;
47   }
48   // object sender 就是传递发送通知的对象, 而EventArgs 是包含事件的类。
49   public void Run(object sender, CatShoutEventArgs args)
50   {
51     Console.WriteLine(" 老猫{0} 来了,{1} 快跑", args.Name, name);
52   }
53 }
54 
55 static void Main(string[] args)
56 {
57   Cat cat = new Cat("Tom");
58   Mouse mouse1 = new Mouse("Jerry");
59   Mouse mouse2 = new Mouse("Jack");
60 
61   // 表示将 Mouse 的 Run 方法通过实例化委托 Cat.CatShoutEventHandler 登记到 Cat 的事件 CatShout 当中。
62   // 其中 “+=” 表示 “add_CatShout” 的意思。
63   cat.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);
64   cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);
65 
66   cat.Shout();
67 }

 

posted @ 2015-06-24 22:32  壬子木  阅读(110)  评论(0)    收藏  举报