C#学习 22 事件 下

事件声明的完整格式

概念:
事件是基于委托的

  • 事件需要使用委托类型来做约束,这种约束既规定了事件能够发送什么样的消息给事件的响应者,也规定了事件的响应者能收到什么样的事件消息,这就决定了事件响应者的事件处理器必须能够和这个约束匹配上,他才能订阅这个事件。
  • 当事件的响应者向事件的拥有者提供了能够匹配这个事件的事件处理器之后呢,你总得找个地方把这个事件处理器保存记录,能够记录或者引用方法的任务只有委托类型的实例才能做到。

很经典的餐馆点菜的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += waiter.Action;
            customer.Action();
            customer.PayTheBill();

        }
    }

    public class OrderEventArgs:EventArgs //声明下菜单表,就是事件信息的格式
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    //委托纸和笔记录下哪桌的客户点了什么菜
    public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);

    public class Customer //事件的拥有者
    {
        private OrderEventHandler orderEventHandler;

        public event OrderEventHandler Order
        {
            add
            {
                this.orderEventHandler += value;
            }
            remove
            {
                this.orderEventHandler -=value;
            }
        }
        public double Bill { get; set; }
        public void PayTheBill()
        {
            Console.WriteLine("I will pay ${0}",this.Bill);
        }

        public void WalkIn()
        {
            Console.WriteLine("Walk into the restaurant");
        }

        public void SitDown()
        {
            Console.WriteLine("Sit Down");
        }

        public void Think()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Let me think...");
                Thread.Sleep(2000);
                
            }

            if (this.orderEventHandler != null)//等于空,就是没人订阅你的事件,就是服务员比较忙,没人响应你
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "Kongpao Chicken";
                e.Size = "large";
                this.orderEventHandler.Invoke(this, e);
            }
        }
        public void Action()
        {
            Console.ReadLine();
            this.WalkIn();
            this.SitDown();
            this.Think();
    }
    }

   

    public class Waiter //事件的响应者
    {
        public void Action(Customer customer, OrderEventArgs e)//响应的对象以及响应的事件消息
        {
            Console.WriteLine("I will serve you the dish-{0}",e.DishName);
            double price = 10;
            
            switch (e.Size)
            {
                case "small":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }

            customer.Bill += price;
        }
    }
}

简略声明

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += waiter.Action;
            customer.Action();
            customer.PayTheBill();

        }
    }

    public class OrderEventArgs:EventArgs //声明下菜单表,就是事件信息的格式
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    //委托纸和笔记录下哪桌的客户点了什么菜
    public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);

    public class Customer //事件的拥有者
    {
        public event OrderEventHandler Order;

        public double Bill { get; set; }
        public void PayTheBill()
        {
            Console.WriteLine("I will pay ${0}",this.Bill);
        }

        public void WalkIn()
        {
            Console.WriteLine("Walk into the restaurant");
        }

        public void SitDown()
        {
            Console.WriteLine("Sit Down");
        }

        public void Think()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Let me think...");
                Thread.Sleep(2000);
                
            }

            if (this.Order != null)//等于空,就是没人订阅你的事件,就是服务员比较忙,没人响应你
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "Kongpao Chicken";
                e.Size = "large";
                this.Order.Invoke(this, e);
            }
        }
        public void Action()
        {
            Console.ReadLine();
            this.WalkIn();
            this.SitDown();
            this.Think();
    }
    }

   

    public class Waiter //事件的响应者
    {
        public void Action(Customer customer, OrderEventArgs e)//响应的对象以及响应的事件消息
        {
            Console.WriteLine("I will serve you the dish-{0}",e.DishName);
            double price = 10;
            
            switch (e.Size)
            {
                case "small":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }

            customer.Bill += price;
        }
    }
}

有了委托字段/属性,为什么还需要事件?
为了程序的逻辑更加有道理和安全

事件的本质就是委托字段的一个包装器

EventHandler委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer(); //事件的拥有者
            Waiter waiter = new Waiter(); //事件的响应者
            customer.Order += waiter.Action; //Customer发了一个Order事件
            customer.Action();
            customer.PayTheBill();

        }
    }

    public  class OrderEventArgs:EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

//    public delegate void OrderEventHandler(Customer customer,OrderEventArgs e);

    public class Customer
    {
        public event EventHandler Order;
        public double Bill { get; set; }
        public void PayTheBill()
        {
            Console.WriteLine("I will pay ${0}",this.Bill);
        }

        public void WalkIn()
        {
            Console.WriteLine("Walk into the restaurant");
        }
        public void SitDown()
        {
            Console.WriteLine("Sit Down");

        }
        public void Think()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Let me think ...");
                Thread.Sleep(1000);
            }

            if (this.Order != null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "Kongpao Chicken";
                e.Size = "large";
                this.Order.Invoke(this, e);
            }
        }

        public void Action()
        {
            Console.ReadLine();
            this.WalkIn();
            this.SitDown();
            this.Think();
        }
    }

    public class Waiter
    {
        public void Action(object sender, EventArgs e)
        {
            Customer customer = sender as Customer;
            OrderEventArgs orderInfo = e as OrderEventArgs;
            Console.WriteLine("I will serve you the dish {0}",orderInfo.DishName);
            double price = 10;
            switch (orderInfo.Size)
            {
                case "small":
                price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }

            customer.Bill += price;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值