1定义和使用委托 

 

 1    class program
 2    {
 3        //1定义委托
 4        delegate Person CreatePersonHandle(string FirstName, string LastName, int Age);
 5        delegate void ShowEmployeeHandle(Employee emp);
 6
 7        static void Main(string[] args)
 8        {
 9            //3委托的使用
10            CreatePersonHandle eh = CreateEmployee;
11            Employee emee = (Employee)eh("Pony""Smith"41);//委托调用
12
13            ShowEmployeeHandle seh = ShowPerson;
14            seh(emee);
15
16        }

17        //2定义要被委托的处理方法,声明与委托一致
18        static Employee CreateEmployee(string FirstName, string LastName, int Age)
19        {
20            return new Employee(FirstName, LastName, Age);
21        }

22        static void ShowPerson(Person p)
23        {
24            Console.WriteLine("姓名:{0} {1} 年龄:{2}", p.firstName, p.lastName, p.age);
25        }

26
27        class Person
28        {
29            public string firstName;
30            public string lastName;
31            public int age;
32            public Person(string a, string b, int c)
33            {
34                firstName = a;
35                lastName = b;
36                age = c;
37            }

38        }

39        class Employee : Person
40        {
41            public Employee(string a, string b, int c)
42                : base(a, b, c)
43            {
44            }

45        }

46
47    }

 

 2使用委托和事件

 

 1    class program
 2    {
 3
 4        static void Main(string[] args)
 5        {
 6            //4使用委托和事件
 7            Point point = new Point();
 8            point.Create += new Point.OnCreate(pnt_Create);
 9            //5此时调用了Point的Create事件时自动调用我们自定义的pnt_Create
10        }

11        //3定义要添加的事件处理
12        static void pnt_Create(Object sender, EventArgs e)
13        {
14            Console.WriteLine("OK");
15        }

16
17        class Point
18        {
19            //使用委托和事件
20            //1首先先定义委托
21            public delegate void OnCreate(Object Sender, EventArgs e);
22            //2创建事件
23            private static event OnCreate _Create;
24            public event OnCreate Create
25            {
26                add { _Create += value; }
27                remove { _Create -= value; }
28            }

29        }

30    }

 

 3类内部实现事件处理

 

 1    class program
 2    
 1
    class Program
 2    {
 3        //类中实现事件处理
 4        static void Main(String[] args)
 5        {
 6            Retail myRetail = new Retail();
 7            Goods a = new Goods("鼠标"12.53);
 8            Goods b = new Goods("机箱"1001);
 9            Goods c = new Goods("键盘"601);
10            myRetail.AddGoods(a);
11            myRetail.AddGoods(b);
12            myRetail.AddGoods(c);
13            myRetail.GoodsPay = 200;
14            Console.WriteLine("共{0}件 合计{1:c}元 支付{2:c}元 找零 {3:c}元", myRetail.GoodsQuantity, myRetail.GoodsPay, myRetail.GoodsMoney, myRetail.Change);
15            Console.ReadLine();
16        }

17    }

18
19        class Retail
20        {
21            private ArrayList GoodsList;
22            private delegate void AddHandle(Goods e);
23            private delegate void PayHandle(double e);
24            private event AddHandle WhenADD;
25            private event PayHandle WhenPay;
26            public int GoodsQuantity;
27            public double GoodsMoney;//商品金额
28            public double Change;//找零
29            private double _GoodsPay;
30            public double GoodsPay//付款信息
31            {
32                get return _GoodsPay; }
33                set
34                {
35                    _GoodsPay = value;
36                    WhenPay(_GoodsPay);//计算金额
37                }

38            }

39
40            public Retail()
41            {
42                GoodsList = new ArrayList();
43                WhenPay = new PayHandle(OnPay);
44                WhenADD = new AddHandle(OnADD);
45            }

46
47            public void AddGoods(Goods AdditionGoods)
48            {
49                GoodsList.Add(AdditionGoods);
50                WhenADD(AdditionGoods);
51            }

52
53            private void OnADD(Goods e)
54            {
55                GoodsMoney += e.Price * e.Quantity;
56                GoodsQuantity += e.Quantity;
57                Console.WriteLine("商品名:{0,-10} | 价格: {1,8:f2} | 数量:{2}", e.GoodsName, e.Price, e.Quantity);
58                Console.WriteLine("-".PadLeft(50'-'));
59            }

60            private void OnPay(double e)
61            {
62                _GoodsPay = e;
63                Change = _GoodsPay - GoodsMoney;
64            }

65        }

66
67        struct Goods//商品结构
68        {
69            public string GoodsName;
70            public double Price;
71            public int Quantity;
72            public Goods(string goodsname, double price, int quantity)
73            {
74                GoodsName = goodsname;
75                Price = price;
76                Quantity = quantity;
77            }

78        }