using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Event { class Program { static void Main(string[] args) { Customer customer = new Customer(); FuWuYuan fuWuYuan = new FuWuYuan(); customer.Order += fuWuYuan.Action; customer.Action(); customer.FuQian(); } } // 养成习惯事件参数继承自EventArgs public class OrderEventArgs : EventArgs { /// <summary> /// 菜名 /// </summary> public string DishName { get; set; } /// <summary> /// 分量 /// </summary> public string Size { get; set; } } // 声明事件参数为事件拥有者Customer和事件参数OrderEventArgs // 代码规范 EventHandler后缀标识用来声明事件的 public delegate void OrderEventHandler(Customer customer,OrderEventArgs e); public class Customer { // 声明事件字段 private OrderEventHandler orderEventHandler; // 添加事件 public event OrderEventHandler Order { add { this.orderEventHandler += value; } remove { this.orderEventHandler -= value; } } public double Bill { get; set; } public void FuQian() { Console.WriteLine("我付了${0}元钱", this.Bill); } public void JinMen() { Console.WriteLine("我进入饭店!"); } public void ZuoXia() { Console.WriteLine("找位置坐下"); } public void DianCai() { Console.WriteLine("拿起菜单......"); for (int i = 0; i < 5; i++) { Console.WriteLine("思考中......"); } if (this.orderEventHandler != null) { OrderEventArgs e = new OrderEventArgs(); e.DishName = "番茄炒鸡蛋"; e.Size = "Max"; this.orderEventHandler.Invoke(this,e); } } public void Action() { Console.ReadLine(); this.JinMen(); this.ZuoXia(); this.DianCai(); } } public class FuWuYuan { public void Action(Customer customer, OrderEventArgs e) { Console.WriteLine("记录了一个菜{0}", e.DishName); double qian = 10; switch (e.Size) { case "Max": qian = qian * 1.5; break; case "Mini": qian = qian * 0.5; break; default: break; } customer.Bill += qian; } } }
标签:customer,Console,自定义,qian,对象,void,事件,using,public From: https://www.cnblogs.com/instal/p/17161500.html