【C#】【Event_03】事件总结与最终代码

本文通过一个餐厅点餐系统的例子,详细介绍了C#中的事件机制,包括事件的声明、触发及订阅过程。展示了如何定义事件处理器、事件参数类,并演示了事件的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考 刘铁猛《C#语言入门详解》

在这里插入图片描述
在这里插入图片描述

Final Example

using System;
using System.Threading;

//事件声明的完整方式

//事件的五个部分
//+ 事件的拥有者
//+ 事件
//+ 事件的响应者 
//+ 事件的处理器
//+ 事件的订阅

//参考 --- 刘铁猛《C#语言入门详解》全集

namespace Event
{
    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 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;
        }
    }

    // [.Net] 传递事件信息的类 ,建议使用事件信息术语 EventArgs 作为后缀 , 派生自 EventArgs 类
    public class OrderEventArgs : EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    public class Customer
    {
        // 编译后会自动生成委托类型字段 Order
        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);
            }

            this.OnOrder("Chicken", "large");
      
        }

        // 使用protected保护方法
        protected void OnOrder(string dishName , string size)
        {
            if (this.Order != null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = dishName;
                e.Size = size;

                // 事件触发
                this.Order.Invoke(this, e);
            }
        }

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值