用向老婆交工资学习代理委托事件!
引子http://www.cnblogs.com/chu888chu888/archive/2007/01/25/630403.html
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication2
6{
7 //这个委托用来说明处理发工资事件的方法的方法头
8 public delegate void ShowMoneyHandler(string s);
9 public class Man
10 {
11 //声明事件
12
13 public event ShowMoneyHandler myMoney;
14 //激发事件的方法
15 public void OnTakeAll(string s)
16 {
17 if (myMoney != null)
18 {
19 myMoney(s);
20 }
21 }
22 //开工资时
23 public void TakeMyMoney(string s)
24 {
25
26 Console.WriteLine("--------------开工资了(Man)--------------------");
27 Console.WriteLine("这个月真爽拿到了{0}元大洋,总算可以换T60了", s);
28 Console.WriteLine("但是银行卡不在我手上,开钱就没有钱,转帐事件在发钱那一刻就激发了!");
29 Console.WriteLine("开工资这事,天知道地知道我知道");
30 Console.WriteLine("对于老婆与老妈她们都是自动知道的!!");
31 OnTakeAll(s);
32
33 }
34 }
35
36 public class Woman
37 {
38 public void reTakeAll(string s)
39 {
40 Console.WriteLine("--------------开工资了(Woman)--------------------");
41 Console.WriteLine("我老公真厉害,这个月开了{0}元大洋哪",s);
42 Console.WriteLine("我爱死你了,T60就先别买了!");
43 }
44 }
45
46 public class Mama
47 {
48 public void TalkToSon(string s)
49 {
50 Console.WriteLine("--------------开工资了(Mama)--------------------");
51 Console.WriteLine("儿子你这个月开了{0}元",s);
52 Console.WriteLine("我怎么一分钱也没有看到哪?");
53 }
54
55 }
56 public class main
57 {
58 static void Main()
59 {
60 Man chu = new Man();
61 Woman li = new Woman();
62 Mama mymama = new Mama();
63
64 chu.myMoney += new ShowMoneyHandler(li.reTakeAll);
65 chu.myMoney+=new ShowMoneyHandler(mymama.TalkToSon);
66 //chu.myMoney+=new ShowMoneyHandler(chu.TakeMyMoney);
67 chu.TakeMyMoney("8000");
68 }
69 }
70}
71
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication2
6{
7 //这个委托用来说明处理发工资事件的方法的方法头
8 public delegate void ShowMoneyHandler(string s);
9 public class Man
10 {
11 //声明事件
12
13 public event ShowMoneyHandler myMoney;
14 //激发事件的方法
15 public void OnTakeAll(string s)
16 {
17 if (myMoney != null)
18 {
19 myMoney(s);
20 }
21 }
22 //开工资时
23 public void TakeMyMoney(string s)
24 {
25
26 Console.WriteLine("--------------开工资了(Man)--------------------");
27 Console.WriteLine("这个月真爽拿到了{0}元大洋,总算可以换T60了", s);
28 Console.WriteLine("但是银行卡不在我手上,开钱就没有钱,转帐事件在发钱那一刻就激发了!");
29 Console.WriteLine("开工资这事,天知道地知道我知道");
30 Console.WriteLine("对于老婆与老妈她们都是自动知道的!!");
31 OnTakeAll(s);
32
33 }
34 }
35
36 public class Woman
37 {
38 public void reTakeAll(string s)
39 {
40 Console.WriteLine("--------------开工资了(Woman)--------------------");
41 Console.WriteLine("我老公真厉害,这个月开了{0}元大洋哪",s);
42 Console.WriteLine("我爱死你了,T60就先别买了!");
43 }
44 }
45
46 public class Mama
47 {
48 public void TalkToSon(string s)
49 {
50 Console.WriteLine("--------------开工资了(Mama)--------------------");
51 Console.WriteLine("儿子你这个月开了{0}元",s);
52 Console.WriteLine("我怎么一分钱也没有看到哪?");
53 }
54
55 }
56 public class main
57 {
58 static void Main()
59 {
60 Man chu = new Man();
61 Woman li = new Woman();
62 Mama mymama = new Mama();
63
64 chu.myMoney += new ShowMoneyHandler(li.reTakeAll);
65 chu.myMoney+=new ShowMoneyHandler(mymama.TalkToSon);
66 //chu.myMoney+=new ShowMoneyHandler(chu.TakeMyMoney);
67 chu.TakeMyMoney("8000");
68 }
69 }
70}
71
posted on 2007-01-25 16:20 项