当前位置: 首页 > article >正文

C#自定义事件的案例

方法一,详细的声明

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;
        }
    }
}

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

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;
        }
    }
}


http://www.kler.cn/a/370660.html

相关文章:

  • Flink (九):DataStream API (六) Process Function
  • 【React】插槽渲染机制
  • git操作(Windows中GitHub)
  • thinkphp6 + redis实现大数据导出excel超时或内存溢出问题解决方案
  • 处理 SQL Server 中的表锁问题
  • 【网络编程】基础知识
  • 前端阻止用户调试(禁用F12,禁用右键菜单,禁用查看源代码,禁用复制,无限debugger断点)
  • 【Linux 从基础到进阶】高负载系统的优化与维护
  • Java学习Day51:紫云山金丹培育基地(移动端开发之多表联查,发送短信验证码)
  • Spring Task—定时任务
  • 钉钉日常报销单与金蝶云星空集成技术详解
  • springboot配置websocket
  • 2025秋招八股文--RPC篇
  • 深入理解TCP——面试20问
  • win docker desktop踩坑及解决方案(拉取镜像失败)
  • 前端对一个增删改查的思考
  • 【机器学习】多项式回归
  • 实战OpenCV之深度学习
  • <大厂实战场景> ~ flutter鸿蒙next处理后端返回来的数据的转义问题
  • 大数据-186 Elasticsearch - ELK 家族 Logstash Input插件 JDBC syslog
  • SSRF服务端请求伪造
  • Pandas 数据分析基础操作:从创建到统计的实用指南
  • 人工智能与机器学习相关算法介绍
  • 掌握机器学习中的偏差与方差:模型性能的关键
  • DAPT: Distribution-Aware Prompt Tuning for Vision-Language Models
  • 实现梦想:Spring Boot驱动的摄影工作室网站