简单的事件示例代码
简单的事件示例代码:
完整形式
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; /* * 事件模型的五个组成部分 * 1. 事件的拥有者(event source, 对象) * 2. 事件的成员(event, 成员) * 3. 事件的响应者(event subscriber, 对象) * 4. 事件的处理器(event handler, 成员) * 5. 事件订阅 --- 把事件处理器与事件关联在一起,本质上是一种以委托类型为基础的“约定” */ namespace ConsoleAppTestDemo1 { // 委托类型声明 public delegate void OrderEventHandler(Customer customer, OrderEventArgs e); // 事件的参数 public class OrderEventArgs : EventArgs { public string DishName { get; set; } public string Size { get; set; } } // 事件的拥有者(event source) --- 客人 public class Customer { // 委托类型的字段 private OrderEventHandler ordereventHandler; // 事件成员(event) --- 委托字段的包装器,对委托字段的访问起限制作用 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 ${this.Bill}"); } // 思考点菜 public void ThinkDishes() { for (int i = 0; i < 5; i++) { Console.WriteLine("Let me think"); Thread.Sleep(1000); } this.OnOrder("Kongpao Chicken", "large"); } // 触发事件,当存在事件的处理器时,处理该事件 --- 访问级别为 protected 不能为 public protected void OnOrder(string dishName, string size) { if (this.ordereventHandler != null) { OrderEventArgs e = new OrderEventArgs() { DishName = dishName, Size = size }; this.ordereventHandler.Invoke(this, e); } } // 开始点菜,触发事件 public void Action() { Console.WriteLine("start order dishes ..."); this.ThinkDishes(); } } // 事件的响应者(event subscriber) --- 服务员 public class Waiter { // 事件处理器(event handler) --- 上菜计费 public void Action(Customer customer, OrderEventArgs e) { Console.WriteLine($"I will server you then dish - {e.DishName} {e.Size}"); 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; } } class TestDemo1 { static void Main(string[] args) { Customer customer = new Customer(); Waiter waiter = new Waiter(); // 事件订阅 customer.Order += new OrderEventHandler(waiter.Action); // 点菜 -- 有服务员来处理 customer.Action(); // 付款 customer.PayTheBill(); // 点菜 -- 有服务员来处理 customer.Action(); // 付款 customer.PayTheBill(); // 取消订阅 customer.Order -= new OrderEventHandler(waiter.Action); // 点菜 --- 没有服务员来处理 customer.Action(); // 付款 customer.PayTheBill(); Console.ReadLine(); } } }
简单形式
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; /* * 事件模型的五个组成部分 * 1. 事件的拥有者(event source, 对象) * 2. 事件的成员(event, 成员) * 3. 事件的响应者(event subscriber, 对象) * 4. 事件的处理器(event handler, 成员) * 5. 事件订阅 --- 把事件处理器与事件关联在一起,本质上是一种以委托类型为基础的“约定” */ namespace ConsoleAppTestDemo1 { // 事件的参数 public class OrderEventArgs : EventArgs { public string DishName { get; set; } public string Size { get; set; } } // 事件的拥有者(event source) --- 客人 public class Customer { // 事件成员(event) --- 委托字段的包装器,对委托字段的访问起限制作用 public event EventHandler<OrderEventArgs> Order; // 需要支付的金额 public double Bill { get; set; } // 付款 public void PayTheBill() { Console.WriteLine($"I will pay ${this.Bill}"); } // 思考点菜 public void ThinkDishes() { for (int i = 0; i < 5; i++) { Console.WriteLine("Let me think"); Thread.Sleep(1000); } this.OnOrder("Kongpao Chicken", "large"); } // 触发事件,当存在事件的处理器时,处理该事件 protected void OnOrder(string dishName, string size) { if (this.Order != null) { OrderEventArgs e = new OrderEventArgs() { DishName = dishName, Size = size }; this.Order.Invoke(this, e); } } // 开始点菜,触发事件 public void Action() { Console.WriteLine("start order dishes ..."); this.ThinkDishes(); } } // 事件的响应者(event subscriber) --- 服务员 public class Waiter { // 事件处理器(event handler) --- 上菜计费 public void Action(object sender, OrderEventArgs e) { Customer customer = sender as Customer; Console.WriteLine($"I will server you then dish - {e.DishName} {e.Size}"); 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; } } class TestDemo1 { static void Main(string[] args) { Customer customer = new Customer(); Waiter waiter = new Waiter(); // 事件订阅 customer.Order += waiter.Action; // 点菜 -- 有服务员来处理 customer.Action(); // 付款 customer.PayTheBill(); // 点菜 -- 有服务员来处理 customer.Action(); // 付款 customer.PayTheBill(); // 取消订阅 customer.Order -= waiter.Action; // 点菜 --- 没有服务员来处理 customer.Action(); // 付款 customer.PayTheBill(); Console.ReadLine(); } } }
输出信息:
start order dishes ... Let me think Let me think Let me think Let me think Let me think I will server you then dish - Kongpao Chicken large I will pay $15 start order dishes ... Let me think Let me think Let me think Let me think Let me think I will server you then dish - Kongpao Chicken large I will pay $30 start order dishes ... Let me think Let me think Let me think Let me think Let me think I will pay $30 请按任意键继续. . .
============ End