初学事件Event(二)
来源:互联网 发布:windows 7 sp1补丁 编辑:程序博客网 时间:2024/06/11 14:45
事件的完整声明:(刘铁猛老师视频示例)
按照事件的5要素去写
using System;using System.Threading;namespace EventFullDeclaration{ class Program { static void Main(string[] args) { Customer customer = new Customer(); Waiter waiter = new Waiter(); customer.Order += waiter.Action; customer.Action(); } } //declare a delegate type public delegate void OrderEventHandler(Customer customer,OrderEventArgs e); public class OrderEventArgs :EventArgs { public string DishName { get; set; } public string Size { get; set; } } //Customer类 事件的拥有者 以及 事件成员 //触发事件由事件拥有者的内部逻辑触发 public class Customer { //声明一个委托类型的字段 private OrderEventHandler 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 you ${0}",this.Bill); } public void WalkIn() { Console.WriteLine("I 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(orderEventHandler != null) { OrderEventArgs e = new OrderEventArgs (); e.DishName = "Pork"; e.Size = "large"; this.orderEventHandler (this , e); this.orderEventHandler.Invoke(this, e); } } public void Action() { this.WalkIn(); this.SitDown(); this.Think(); } } public class Waiter { //事件处理器 public void Action(Customer customer, OrderEventArgs e) { Console.WriteLine("I will sever you the dish : {0}",e.DishName ); double price = 10; switch (e.Size ) { case "large": price *= 1.5; break; case "small": price *= 0.5; break; default: break; } customer.Bill += price; } }}
0 0
- 初学事件Event(二)
- 事件(event)设计准则(二)
- libevent事件(二)---event源码
- 温故知新(8)委托(二)事件 event
- SharePoint Workflow架构(二)Workflow 事件处理(Event Processing)
- 事件(event)
- 事件(Event)
- event事件(1)
- 创建事件(event)
- 事件(event)
- MySQL事件(Event)
- C# 事件(Event)
- C# 事件(Event)
- C#事件(Event)
- MySQL事件(Event)
- C# 事件(Event)
- 事件(Event)
- 【C#】事件(Event)
- Struts2中Session的使用方法详细解释
- 修改一个方法,应针对所有可能影响到的地方进行测试
- 完美调用系统相机拍照,获取图片进行压缩并本地保存
- 创建测试用例的代码
- php单引号与双引号用法
- 初学事件Event(二)
- 博客的开始,技术的积累
- php7下的opcache特性导致的一个bug排查
- 网页爬去 String转为Html形式
- Android 集成支付宝SDK实现快捷支付--详解
- MYSQL数据库远程登录及权限设置
- jquery 滚动条事件
- linux命令备忘
- 数据库查询特殊字符&的处理方法