C#自定义事件的案例

方法一,详细的声明

cs 复制代码
namespace HelloWorldConsole
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += waiter.Action;
            customer.Action();
        }
    }

    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 double Bill { get; set; }
        private OrderEventHandler orderEventHandler;

        public event OrderEventHandler Order
        {
            add { orderEventHandler += value; }
            remove { orderEventHandler -= value; }
        }

        public void PayTheBill()
        {
            Console.WriteLine("i will pay-{0}", Bill);
        }

        public void Think()
        {
            if (orderEventHandler != null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "宫保鸡丁";
                e.Size = "large";
                orderEventHandler(this, e);
            }
        }

        public void Action()
        {
            Think();
            PayTheBill();
        }
    }

    public class Waiter
    {
        public void Action(Customer customer, OrderEventArgs e)
        {
            double price = 10;
            switch (e.Size)
            {
                case "small": price /= 2; break;
                case "large": price *= 1.5; break;
                default: break;
            }
            Console.WriteLine("i will serve the dish {0}-{1}", e.DishName, e.Size);
            customer.Bill += price;
        }
    }
}

方法二,简化的声明,也是最常用的写法

cs 复制代码
namespace HelloWorldConsole
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += waiter.Action;
            customer.Action();
        }
    }

    /// <summary>
    /// 事件参收
    /// </summary>
    public class OrderEventArgs : EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    public class Customer
    {
        public double Bill { get; set; }
        public event EventHandler Order;

        public void Think()
        {
            if (Order != null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "宫保鸡丁";
                e.Size = "large";
                Order(this, e);
            }
        }

        public void PayTheBill()
        {
            Console.WriteLine("i will pay-{0}", Bill);
        }

        public void Action()
        {
            Think();
            PayTheBill();
        }
    }

    public class Waiter
    {
        public void Action(object sender, EventArgs _e)
        {
            Customer customer = sender as Customer;
            OrderEventArgs e = _e as OrderEventArgs;
            double price = 10;
            switch (e.Size)
            {
                case "small": price /= 2; break;
                case "large": price *= 1.5; break;
                default: break;
            }
            Console.WriteLine("i will serve the dish {0}-{1}", e.DishName, e.Size);
            customer.Bill += price;
        }
    }
}
相关推荐
2401_8574396927 分钟前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Dream_Snowar2 小时前
速通Python 第三节
开发语言·python
高山我梦口香糖3 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
信号处理学渣3 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客3 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
jasmine s3 小时前
Pandas
开发语言·python
biomooc4 小时前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)
开发语言·r语言
骇客野人4 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言