C#笔记⑥v2——自定义事件
一、完整自定义事件
完整自定义事件就会用到自定义的委托
事件就是用来保护委托的
用delegate声明委托时,一般采用事件名字+EventHandle后加(事件拥有者,事件数据)
而事件数据(事件名+EventArgs),在事件数据类中,最好继承EventArgs
而我们的核心,第一就是要搞懂,事件和委托不是一回事,事件用来限制委托的,不是一个委托类型字段,这就导致委托是被事件约束的。
第二就是当我们事件是怎么被触发的,只有当事件的响应者响应的时候,才会触发我们的事件,这时候我们的事件才有把有效值交给我们的委托。
第三就是我们事件拥有者,经一些内部算法,将值传给委托。
第四就是我们的事件响应者也就是订阅后,看到委托传来,执行委托。
完整自定义事件
class Test
{
static void Main(string[] args)
{
Customer customer=new Customer();
Waiter waiter=new Waiter();
customer.Order+=waiter.Action;
customer.Action();
customer.PayBill();
Console.ReadLine();
}
}
public class OrderEventArgs:EventArgs
{
public string DishName { get; set; }
public string Size { get; set; }
}
public delegate void OrderEventHandle(Customer customer,OrderEventArgs e);
public class Customer
{
private OrderEventHandle orderEventHandle;
public event OrderEventHandle Order //用什么委托约束事件
{
add
{
this.orderEventHandle+=value; //第二就是当我们事件是怎么被触发的,只有当事件的响应者响应的时候,才会触发我们的事件,这时候我们的事件才有把有效值交给我们的委托。
}
remove
{
this.orderEventHandle-=value;
}
}
public double Bill { get; set; }
public void PayBill()
{
System.Console.WriteLine("I will pay for {0}",this.Bill);
}
public void Walk()
{
System.Console.WriteLine("Walking");
}
public void Tores()
{
System.Console.WriteLine("Come");
}
public void Think()
{
for(int i=0;i<5;i++)
{
System.Console.WriteLine("I am Thinking");
Thread.Sleep(500);
}
if(orderEventHandle!=null)
{
this.OnOrder("lamian","Large");
}
}
protected void OnOrder(string dishname,string size)
{
OrderEventArgs e=new OrderEventArgs();
e.DishName=dishname;
e.Size=size;
this.orderEventHandle(this,e); //第三就是我们事件拥有者,经一些内部算法,将值传给委托
}
public void Action()
{
this.Walk();
this.Tores();
this.Think();
Console.ReadLine();
}
}
class Waiter
{
public void Action(Customer customer,OrderEventArgs e)
{
System.Console.WriteLine("I will serve you {0}.!",e.DishName);
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 Test
{
static void Main(string[] args)
{
Customer customer=new Customer();
Waiter waiter=new Waiter();
customer.Order+=waiter.Action;
customer.Action();
customer.PayBill();
Console.ReadLine();
}
}
public class OrderEventArgs:EventArgs
{
public string DishName { get; set; }
public string Size { get; set; }
}
public delegate void OrderEventHandle(Customer customer,OrderEventArgs e);
public class Customer
{
public event OrderEventHandle Order; //事件不是委托类型字段
public double Bill { get; set; }
public void PayBill()
{
System.Console.WriteLine("I will pay for {0}",this.Bill);
}
public void Walk()
{
System.Console.WriteLine("Walking");
}
public void Tores()
{
System.Console.WriteLine("Come");
}
public void Think()
{
for(int i=0;i<5;i++)
{
System.Console.WriteLine("I am Thinking");
Thread.Sleep(500);
}
if(this.Order!=null)
{
this.OnOrder("lamian","Large");
}
}
protected void OnOrder(string dishname,string size)
{
OrderEventArgs e=new OrderEventArgs();
e.DishName=dishname;
e.Size=size;
this.Order(this,e); //第三就是我们事件拥有者,经一些内部算法,将值传给委托
}
public void Action()
{
this.Walk();
this.Tores();
this.Think();
Console.ReadLine();
}
}
class Waiter
{
public void Action(Customer customer,OrderEventArgs e)
{
System.Console.WriteLine("I will serve you {0}.!",e.DishName);
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;
}
}
三、不用自定义委托,用EventHandler
点击查看代码
class Test
{
static void Main(string[] args)
{
Customer customer=new Customer();
Waiter waiter=new Waiter();
customer.Order+=waiter.Action;
customer.Action();
customer.PayBill();
Console.ReadLine();
}
}
public class OrderEventArgs:EventArgs
{
public string DishName { get; set; }
public string Size { get; set; }
}
public class Customer
{
public event EventHandler Order; //此处有修改
public double Bill { get; set; }
public void PayBill()
{
System.Console.WriteLine("I will pay for {0}",this.Bill);
}
public void Walk()
{
System.Console.WriteLine("Walking");
}
public void Tores()
{
System.Console.WriteLine("Come");
}
public void Think()
{
for(int i=0;i<5;i++)
{
System.Console.WriteLine("I am Thinking");
Thread.Sleep(500);
}
if(this.Order!=null)
{
this.OnOrder("lamian","Large");
}
}
protected void OnOrder(string dishname,string size)
{
OrderEventArgs e=new OrderEventArgs();
e.DishName=dishname;
e.Size=size;
this.Order(this,e); //第三就是我们事件拥有者,经一些内部算法,将值传给委托
}
public void Action()
{
this.Walk();
this.Tores();
this.Think();
Console.ReadLine();
}
}
class Waiter
{
public void Action(object sender,EventArgs e) //此处有修改
{
Customer customer=sender as Customer; //如果是Customer就返回此类型给customer
OrderEventArgs oredere=e as OrderEventArgs;
System.Console.WriteLine("I will serve you {0}.!",oredere.DishName);
double Price=10;
switch (oredere.Size)
{
case "Small":
Price=Price*0.5;break;
case "Large":
Price=Price*1.5;break;
default:break;
}
customer.Bill += Price;
}
}
总结:
为什么说事件是基于委托的

浙公网安备 33010602011771号