C#事件
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics.Tracing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace JSONTest 9 { 10 class Class2 11 { 12 static void Main() 13 { 14 Customer customer = new Customer(); 15 Waiter waiter = new Waiter(); 16 customer.Order += waiter.Action; 17 customer.Action(); 18 customer.PayBill(); 19 } 20 } 21 public class OrderEventArgs:EventArgs 22 { 23 public string DishName { get; set; } 24 public string Size { get; set; } 25 26 27 } 28 public delegate void OrderEventHandler(Customer customer, OrderEventArgs e); 29 public class Customer 30 { 31 private OrderEventHandler orderEventHandler; 32 public event OrderEventHandler Order 33 { 34 add 35 { 36 this.orderEventHandler += value; 37 } 38 remove 39 { 40 this.orderEventHandler -= value; 41 } 42 }
//以上为声明事件Order 43 public double Bill { get; set; } 44 public void PayBill() 45 { 46 Console.WriteLine("Pay{0}",this.Bill); 47 } 48 public void WalkIn() 49 { 50 Console.WriteLine("Walk into the restaurant"); 51 } 52 public void SitDown() 53 { 54 Console.WriteLine("SitDown"); 55 } 56 public void Think() 57 { 58 Console.WriteLine("Thinking"); 59 if(this.orderEventHandler!=null) 60 { 61 OrderEventArgs e = new OrderEventArgs(); 62 e.DishName = "鸡丁"; 63 e.Size = "Big"; 64 this.orderEventHandler.Invoke(this, e); 65 } 66 } 67 public void Action() 68 { 69 Console.ReadLine(); 70 WalkIn(); 71 SitDown(); 72 Think(); 73 } 74 } 75 class Waiter 76 { 77 public void Action(Customer customer, OrderEventArgs e) 78 { 79 Console.WriteLine("上菜{0}", e.DishName); 80 double price = 10; 81 switch(e.Size) 82 { 83 case "small": 84 price *= 0.5; 85 break; 86 case "Big": 87 price *= 1.5; 88 break; 89 default: 90 break; 91 } 92 customer.Bill += price; 93 } 94 } 95 }
以上是创建自定义事件的完整格式
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics.Tracing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace JSONTest 9 { 10 class Class2 11 { 12 static void Main() 13 { 14 Customer customer = new Customer(); 15 Waiter waiter = new Waiter(); 16 customer.Order += waiter.Action; 17 customer.Action(); 18 customer.PayBill(); 19 } 20 } 21 public class OrderEventArgs:EventArgs 22 { 23 public string DishName { get; set; } 24 public string Size { get; set; } 25 26 27 } 28 public delegate void OrderEventHandler(Customer customer, OrderEventArgs e); 29 public class Customer 30 { 31 public event OrderEventHandler Order; 32 public double Bill { get; set; } 33 public void PayBill() 34 { 35 Console.WriteLine("Pay{0}",this.Bill); 36 } 37 public void WalkIn() 38 { 39 Console.WriteLine("Walk into the restaurant"); 40 } 41 public void SitDown() 42 { 43 Console.WriteLine("SitDown"); 44 } 45 public void Think() 46 { 47 Console.WriteLine("Thinking"); 48 if(this.Order!=null) 49 { 50 OrderEventArgs e = new OrderEventArgs(); 51 e.DishName = "鸡丁"; 52 e.Size = "Big"; 53 this.Order.Invoke(this, e); 54 } 55 } 56 public void Action() 57 { 58 Console.ReadLine(); 59 WalkIn(); 60 SitDown(); 61 Think(); 62 } 63 } 64 class Waiter 65 { 66 public void Action(Customer customer, OrderEventArgs e) 67 { 68 Console.WriteLine("上菜{0}", e.DishName); 69 double price = 10; 70 switch(e.Size) 71 { 72 case "small": 73 price *= 0.5; 74 break; 75 case "Big": 76 price *= 1.5; 77 break; 78 default: 79 break; 80 } 81 customer.Bill += price; 82 } 83 } 84 }
以上为简略声明事件
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics.Tracing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace JSONTest 9 { 10 class Class2 11 { 12 static void Main() 13 { 14 Customer customer = new Customer(); 15 Waiter waiter = new Waiter(); 16 customer.Order += waiter.Action; 17 customer.Action(); 18 customer.PayBill(); 19 } 20 } 21 public class OrderEventArgs:EventArgs 22 { 23 public string DishName { get; set; } 24 public string Size { get; set; } 25 26 27 } 28 //public delegate void OrderEventHandler(Customer customer, OrderEventArgs e); 29 public class Customer 30 { 31 public event EventHandler Order; //用系统自带的EventHandler声明事件 32 public double Bill { get; set; } 33 public void PayBill() 34 { 35 Console.WriteLine("Pay{0}",this.Bill); 36 } 37 public void WalkIn() 38 { 39 Console.WriteLine("Walk into the restaurant"); 40 } 41 public void SitDown() 42 { 43 Console.WriteLine("SitDown"); 44 } 45 public void Think() 46 { 47 Console.WriteLine("Thinking"); 48 if(this.Order!=null) 49 { 50 OrderEventArgs e = new OrderEventArgs(); 51 e.DishName = "鸡丁"; 52 e.Size = "Big"; 53 this.Order.Invoke(this, e); 54 } 55 } 56 public void Action() 57 { 58 Console.ReadLine(); 59 WalkIn(); 60 SitDown(); 61 Think(); 62 } 63 } 64 class Waiter 65 { 66 public void Action(object sender, EventArgs e) //事件处理器也要符合格式 67 { 68 Customer customer = sender as Customer; //转换 69 OrderEventArgs orderInfo = e as OrderEventArgs; 70 Console.WriteLine("上菜{0}", orderInfo.DishName); 71 double price = 10; 72 switch (orderInfo.Size) 73 { 74 case "small": 75 price *= 0.5; 76 break; 77 case "Big": 78 price *= 1.5; 79 break; 80 default: 81 break; 82 } 83 customer.Bill += price; 84 } 85 } 86 }
以上为不自己声明委托类型,而用系统自带的EventHandler