C# 標準事件流實例代碼
更新時間:2020年07月23日 08:32:37 作者:滑豬小板
這篇文章主要介紹了C# 標準事件流的實例代碼,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
服裝價格變動,觸發(fā)淘寶發(fā)布活動和消費者購買衣服事件流
public class EventStandard { public class Clothes { /// <summary> /// 服裝編碼 /// </summary> public string Id { get; set; } /// <summary> /// 服裝名稱 /// </summary> public string Name { get; set; } /// <summary> /// 服裝價格 /// </summary> private double _price; public double Price { get { return this._price; } set { PriceRiseHandler?.Invoke(this, new PriceEventArgs() { OldPrice = this._price, NewPrice = value }); this._price = value; } } /// <summary> /// 服裝價格變動事件 /// </summary> public event EventHandler PriceRiseHandler; } /// <summary> /// 衣服價格事件參數(shù) 一般會為特定的事件去封裝個參數(shù)類型 /// </summary> public class PriceEventArgs : EventArgs { public double OldPrice { get; set; } public double NewPrice { get; set; } } public class TaoBao { /// <summary> /// 淘寶訂戶 /// </summary> public void PublishPriceInfo(object sender, EventArgs e) { Clothes clothes = (Clothes)sender; PriceEventArgs args = (PriceEventArgs)e; if (args.NewPrice < args.OldPrice) Console.WriteLine($"淘寶:發(fā)布衣服價格下降的公告,{clothes.Name}服裝直降{args.OldPrice - args.NewPrice}元,限時搶購!"); else Console.WriteLine("淘寶:價格悄悄上漲或價格未變化,啥也不做"); } } public class Consumer { /// <summary> /// 消費者訂戶 /// </summary> public void Buy(object sender, EventArgs e) { Clothes clothes = (Clothes)sender; PriceEventArgs args = (PriceEventArgs)e; if (args.NewPrice < args.OldPrice) Console.WriteLine($"消費者:之前價格{args.OldPrice},現(xiàn)在價格{args.NewPrice},果斷買了!"); else Console.WriteLine($"消費者:等等看,降價了再說"); } } public static void Show() { Clothes clothes = new Clothes() { Id = "12111-XK", Name = "優(yōu)衣庫", Price = 128 }; //訂閱:把訂戶和發(fā)布者的事件關(guān)聯(lián)起來 clothes.PriceRiseHandler += new TaoBao().PublishPriceInfo; clothes.PriceRiseHandler += new Consumer().Buy; //價格變化,自動觸發(fā)訂戶訂閱的事件 clothes.Price = 300; } }
調(diào)用:
clothes.Price = 300; EventStandard.Show();
clothes.Price = 98; EventStandard.Show();
以上就是C# 標準事件流實例代碼的詳細內(nèi)容,更多關(guān)于C# 標準事件流的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11c#使用IAsyncEnumerable實現(xiàn)流式分段傳輸
這篇文章主要為大家詳細介紹了c#如何使用IAsyncEnumerable實現(xiàn)流式分段傳輸,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10C# Winform 實現(xiàn)屏蔽鍵盤的win和alt+F4的實現(xiàn)代碼
最近在做一個惡搞程序,就是打開后,程序獲得桌面的截圖然后,然后全屏顯示在屏幕上,用戶此時則不能進行任何操作。2009-02-02C#調(diào)用Windows的API實現(xiàn)窗體動畫
在VF、VB、PB的應(yīng)用中,有些無法通過語言工具本身來完成的或者做得不理想的功能,我們會考慮通過Windows的API來完成。本文就來通過調(diào)用Windows的API實現(xiàn)窗體動畫,感興趣的可以嘗試一下2022-11-11C# Winform實現(xiàn)導(dǎo)出DataGridView當(dāng)前頁以及全部數(shù)據(jù)
基本上,所有的業(yè)務(wù)系統(tǒng)都會要求有導(dǎo)出的功能,所以這篇文章主要為大家介紹了如何利用Winform實現(xiàn)原生DataGridView的導(dǎo)出功能,需要的可以參考一下2023-07-07