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); } } }