[收藏]关于委托事件的一两个很好的例子

第一个例子

public delegate void EatEventHandler(object sender, EatEventArgs e);
这个的定义也可以不要这样.可以直接就定义一个参数
public delegate void EatEventHandler(string e);
但然用的时候的参数要相对应好

 1using System; 
 2
 3namespace nsEventSample 
 4
 5    /// <summary> 
 6    /// 类EatEventArgs 必须继承自类EventArgs,用来引发事件时封装数据 
 7    /// </summary> 

 8    public class EatEventArgs : EventArgs 
 9    
10        public String restrauntName;     //饭店名称 
11        public decimal moneyOut;            //准备消费金额 
12    }
 
13
14    /// <summary> 
15    /// 这个委托用来说明处理吃饭事件的方法的方法头(模式) 
16    /// </summary> 

17    public delegate void EatEventHandler(object sender, EatEventArgs e); 
18
19    /// <summary> 
20    /// 引发吃饭事件(EateEvent)的类Master(主人),这个类必须 
21    /// 1.声明一个名为EatEvent的事件: public event EatEventHandler EatEvent; 
22    /// 2.通过一个名为OnEatEvent的方法来引发吃饭事件,给那些处理此事件的方法传数据; 
23    /// 3.说明在某种情形下引发事件呢?在饿的时候。用方法Hungrg来模拟。 
24    /// </summary> 

25    public class Master 
26    
27        //声明事件 
28        public event EatEventHandler EatEvent; 
29
30        //引发事件的方法 
31        public void OnEatEvent(EatEventArgs e) 
32        
33            if (EatEvent != null
34            
35                EatEvent(this, e); 
36            }
 
37        }
 
38
39        //当主人饿的时候,他会指定吃饭地点和消费金额。 
40        public void Hungry(String  restrauntName, decimal moneyOut) 
41        
42            EatEventArgs e = new EatEventArgs(); 
43            e.restrauntName = restrauntName; 
44            e.moneyOut = moneyOut; 
45
46            Console.WriteLine("主人说:"); 
47            Console.WriteLine("我饿了,要去{0}吃饭,消费{1}元", e.restrauntName, e.moneyOut); 
48
49            //引发事件 
50            OnEatEvent(e); 
51        }
 
52    }
 
53
54    /// <summary> 
55    /// 类Servant(仆人)有一个方法ArrangeFood(安排食物)来处理主人的吃饭事件 
56    /// </summary> 

57    public class Servant 
58    
59        public void ArrangeFood(object sender, EatEventArgs e) 
60        
61            Console.WriteLine(); 
62            Console.WriteLine("仆人说:"); 
63            Console.WriteLine("我的主人, 您的命令是 : "); 
64            Console.WriteLine("吃饭地点 -- {0}", e.restrauntName); 
65            Console.WriteLine("准备消费 -- {0}元 ", e.moneyOut); 
66            Console.WriteLine("好的,正给您安排。。。。。。。。"); 
67            Console.WriteLine("主人,您的食物在这儿,请慢用"); 
68        }
 
69    }
 
70
71    /// <summary> 
72    /// 类God安排qinshihuang(秦始皇)的仆人是lisi(李斯),并让李斯的ArrangeFood 
73    /// 方法来处理qinshihuang的吃饭事件:qinshihuang.EatEvent += new EatEventHandler(lishi.ArrangeFood); 
74    /// </summary> 

75    public class God 
76    
77        public static void Main() 
78        
79            Master  qinshihuang = new Master(); 
80            Servant lishi = new Servant(); 
81
82            qinshihuang.EatEvent += new EatEventHandler(lishi.ArrangeFood); 
83             
84            //秦始皇饿了,想去希尔顿大酒店,消费5000元 
85            qinshihuang.Hungry("希尔顿大酒店"5000.0m); 
86        }
 
87    }
 
88}
 
89

第二个例子来源于:
http://community.csdn.net/Expert/topic/2651/2651579.xml?temp=.7183191

比如说一个公司(场景),你是老板,手下有两个员工,小张和小李。你命令小张注意小李,在开发软件工作的时候如果上网打游戏,你就记录下来,从小李工资里扣100元钱。这个实际上就是现实中的委托。

现在给出一个代码,C#控制台程序,编译运行通过

 1using System;
 2
 3namespace CSharpConsole
 4{
 5 public class 场景
 6 {
 7  [STAThread]
 8  public static void Main(string[] args)
 9  {
10   Console.WriteLine("场景开始了.");
11   // 生成小王
12   小王 w = new 小王();
13   // 生成小账
14   小张 z = new 小张();
15
16   // 指定监视
17   z.PlayGame += new PlayGameHandler(w.扣钱);
18   
19   // 开始玩游戏
20   z.玩游戏();
21
22   Console.WriteLine("场景结束");
23   Console.ReadLine();
24  }

25 }

26
27
28 // 负责扣钱的人
29 public class 小王
30 {
31  public 小王()
32  {
33   Console.WriteLine("生成小王");
34  }

35
36  public void 扣钱(object sender,EventArgs e)
37  {
38   Console.WriteLine("小王:好小子,上班时间胆敢玩游戏");
39   Console.WriteLine("小王:看看你小子有多少钱");
40   小张 f = (小张)sender;
41   Console.WriteLine("小张的钱: " + f.钱.ToString());
42   Console.WriteLine("开始扣钱");
43   System.Threading.Thread.Sleep(500);
44   f.钱 = f.钱 - 500;
45   Console.WriteLine("扣完了.现在小张还剩下:" + f.钱.ToString());
46  }

47 }

48
49 // 如果玩游戏,则引发事件
50 public class 小张
51 {
52  // 先定义一个事件,这个事件表示“小张”在玩游戏。
53  public event PlayGameHandler PlayGame;
54  // 保存小张钱的变量
55  private int m_Money;
56
57  public 小张()
58  {
59   Console.WriteLine("生成小张.");
60   m_Money = 1000// 构造函数,初始化小张的钱。
61  }

62
63  public int 钱 // 此属性可以操作小张的钱。
64  {
65   get
66   {
67    return m_Money;
68   }

69   set
70   {
71    m_Money = value;
72   }

73  }

74
75  public void 玩游戏()
76  {
77   Console.WriteLine("小张开始玩游戏了..");
78   Console.WriteLine("小张:CS好玩,哈哈哈! 我玩..");
79   System.Threading.Thread.Sleep(500);
80   System.EventArgs e = new EventArgs();
81   OnPlayGame(e);
82  }

83
84  protected virtual void OnPlayGame(EventArgs e)
85  {
86   if(PlayGame != null)
87   {
88    PlayGame(this,e);
89   }

90  }

91 }

92
93 // 定义委托处理程序
94 public delegate void PlayGameHandler(object sender,System.EventArgs e);
95}

96

推荐大家一系列文章:
http://blog.joycode.com/percyboy/archive/2005/01/22/43438.aspx

感谢hackate 和  LoveCherry

posted @ 2005-06-20 15:27  冰戈  阅读(922)  评论(1)    收藏  举报