C#中的委托、事件及事件的订阅

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//P80
//事件建立在委托之上,通过该机制,某个类在发生某些特定的事情之后,通知其他类或对象正在发生的事情。

//1.定义价格事件的参数类--PriceChangedEventArgs;
//2.然后,定义该时间段额处理委托类型----PriceChangedEventHander;
//3.在Shop类中定义具体的事件----PiceChanged,它实际上就是PriceChangedEventHander类型的委托;
//4.在OnPriceChanged()中引发PriceChanged事件,将当前类作为事件的发生者,在引发事件之前先判断是否已经注册。
//---------------------------------------------------------------------------------------------------------

namespace Uing_event
{
class Program
{
//①定义价格事件的参数类--PriceChangedEventArgs
public class PriceChangedEventArgs : EventArgs
{
//构造函数
public PriceChangedEventArgs(string nm, float pr)
{
this._Name = nm;
this._Price = pr;
}
//价格变化的商品名称
private string _Name;
public string Name
{
get
{
return this._Name;
}
set
{
this._Name = value;
}
}
//价格变化之后的商品价格
private float _Price;
public float Price
{
get
{
return this._Price;
}
}
}

//②定义价格变化的委托类型
public delegate void PriceChangedEventHandle(object sender,PriceChangedEventArgs e);//定义事件处理的委托类型 △

//商店类,价格变化事件的发生类
//-------------------------------------------------------------------------------------------------------
public class Shop
{
//③通过event关键字定义价格变化事件
public event PriceChangedEventHandle PriceChanged;//它实际上就是PriceChangedEventHander类型的委托

//引发事件函数
protected void OnPriceChanged(PriceChangedEventArgs arg)//在该函数中直接引发PriceChanged事件
{
//如果事件已经注册,则通过委托调用函数的方式通知事件订阅用户
if (this.PriceChanged!=null)//weng:如果事件被订阅
{
this.PriceChanged(this,arg);
}
}

//更新商品名称,并引发商品价格变化事件
public void UpdatePrice(string nm, float newPrice)
{
//创建PriceChanged事件参数
PriceChangedEventArgs arg = new PriceChangedEventArgs(nm, newPrice);
//引发PriceChanged事件
this.OnPriceChanged(arg);
}
public Shop(string nm)
{
this._Name = nm;
}
//商品名称
private string _Name;
public string Name
{
get { return this._Name; }
}
}//end class Shop

//客户类,用于订阅PriceChanged事件
//-------------------------------------------------------------------------------------------------------
public class Customer
{
public Customer(string nm)
{
this._Name = nm;
}
//顾客姓名
private string _Name;
public string Name
{
get { return this._Name; }
}
//PriceChanged事件的响应函数
public void Shop_PriceChanged(object sender, PriceChangedEventArgs e)
{
//将sender转换为Shop类型变量,并打印提示信息
Shop sp = sender as Shop;
if (sp != null)
System.Console.WriteLine("{0}收到{1}:{2}新的价格为{3}元",this._Name,sp.Name,e.Name,e.Price);
}
}

static void Main(string[] args)
{
//定义两个Shop对象,用于引发事件
Shop shop1 = new Shop("南京和平电影城");
Shop shop2 = new Shop("钱塘园");
//定义两个Customer对象,用来订阅并处理事件
Customer cust1 = new Customer("WENG LIU");
Customer cust2 = new Customer("Customer1");

//cust1订阅Shop1的PriceChanged事件,并使用自己的处理函数Shop_PriceChanged处理该事件。
//顾客订阅哪家店铺的事件,实际上就是将本人对该商店的那个事件的处理函数加入那家商店的委托类型变量函数链中
System.Console.WriteLine("1.shop1.UpdataPrice("Goods1",2.2f)");
shop1.PriceChanged += cust1.Shop_PriceChanged;
//shop1更新商品价格,引发PriceChanged事件
shop1.UpdatePrice("Goods1",2.5f);

//cust2订阅Shop2的PriceChanged事件,并使用自己的处理函数Shop_PriceChanged处理该事件。
System.Console.WriteLine("2.shop2.UpdataPrice("Goods2",5.6f)");
shop2.PriceChanged += cust2.Shop_PriceChanged;
//shop1更新商品价格,引发PriceChanged事件
shop2.UpdatePrice("Goods2", 5.6f);

//cust2订阅shop1的shop1的PriceChanged事件
shop1.PriceChanged += cust2.Shop_PriceChanged;
//shop1更新商品价格,引发PriceChanged事件
System.Console.WriteLine("3.shop1.UpdataPrice("Goods3",1.6f)");
shop1.UpdatePrice("Goods3",1.6f);

//csut1取消订阅shop1的PriceChanged事件
shop1.PriceChanged -= cust1.Shop_PriceChanged;
//shop1更新商品价格,引发PriceChanged事件
System.Console.WriteLine("4.shop1.UpdataPrice("Goods4",0.5f)");
shop1.UpdatePrice("Goods4", 1.6f);

}
}
}

  

原文地址:https://www.cnblogs.com/arxive/p/11747898.html