C# 標(biāo)準(zhǔn)事件流實(shí)例代碼
服裝價(jià)格變動(dòng),觸發(fā)淘寶發(fā)布活動(dòng)和消費(fèi)者購買衣服事件流
public class EventStandard { public class Clothes { /// <summary> /// 服裝編碼 /// </summary> public string Id { get; set; } /// <summary> /// 服裝名稱 /// </summary> public string Name { get; set; } /// <summary> /// 服裝價(jià)格 /// </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> /// 服裝價(jià)格變動(dòng)事件 /// </summary> public event EventHandler PriceRiseHandler; } /// <summary> /// 衣服價(jià)格事件參數(shù) 一般會(huì)為特定的事件去封裝個(gè)參數(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ā)布衣服價(jià)格下降的公告,{clothes.Name}服裝直降{args.OldPrice - args.NewPrice}元,限時(shí)搶購!"); else Console.WriteLine("淘寶:價(jià)格悄悄上漲或價(jià)格未變化,啥也不做"); } } public class Consumer { /// <summary> /// 消費(fèi)者訂戶 /// </summary> public void Buy(object sender, EventArgs e) { Clothes clothes = (Clothes)sender; PriceEventArgs args = (PriceEventArgs)e; if (args.NewPrice < args.OldPrice) Console.WriteLine($"消費(fèi)者:之前價(jià)格{args.OldPrice},現(xiàn)在價(jià)格{args.NewPrice},果斷買了!"); else Console.WriteLine($"消費(fèi)者:等等看,降價(jià)了再說"); } } 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; //價(jià)格變化,自動(dòng)觸發(fā)訂戶訂閱的事件 clothes.Price = 300; } }
調(diào)用:
clothes.Price = 300; EventStandard.Show();
clothes.Price = 98; EventStandard.Show();
以上就是C# 標(biāo)準(zhǔn)事件流實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于C# 標(biāo)準(zhǔn)事件流的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11c#使用IAsyncEnumerable實(shí)現(xiàn)流式分段傳輸
這篇文章主要為大家詳細(xì)介紹了c#如何使用IAsyncEnumerable實(shí)現(xiàn)流式分段傳輸,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10基于WPF實(shí)現(xiàn)控件輪廓跑馬燈動(dòng)畫效果
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)控件輪廓跑馬燈動(dòng)畫效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-08-08C#常見應(yīng)用函數(shù)實(shí)例小結(jié)
這篇文章主要介紹了C#常見應(yīng)用函數(shù),結(jié)合實(shí)例形式總結(jié)分析了C#常用的時(shí)間、URL、HTML、反射、小數(shù)運(yùn)算等相關(guān)函數(shù),需要的朋友可以參考下2017-01-01C# Winform 實(shí)現(xiàn)屏蔽鍵盤的win和alt+F4的實(shí)現(xiàn)代碼
最近在做一個(gè)惡搞程序,就是打開后,程序獲得桌面的截圖然后,然后全屏顯示在屏幕上,用戶此時(shí)則不能進(jìn)行任何操作。2009-02-02C#調(diào)用Windows的API實(shí)現(xiàn)窗體動(dòng)畫
在VF、VB、PB的應(yīng)用中,有些無法通過語言工具本身來完成的或者做得不理想的功能,我們會(huì)考慮通過Windows的API來完成。本文就來通過調(diào)用Windows的API實(shí)現(xiàn)窗體動(dòng)畫,感興趣的可以嘗試一下2022-11-11C#使用Queue<T>進(jìn)行隊(duì)列設(shè)計(jì)
Queue<T>類提供了許多方法和屬性,用于處理隊(duì)列中的元素,本文主要介紹了C#使用Queue<T>進(jìn)行隊(duì)列設(shè)計(jì),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03C# Winform實(shí)現(xiàn)導(dǎo)出DataGridView當(dāng)前頁以及全部數(shù)據(jù)
基本上,所有的業(yè)務(wù)系統(tǒng)都會(huì)要求有導(dǎo)出的功能,所以這篇文章主要為大家介紹了如何利用Winform實(shí)現(xiàn)原生DataGridView的導(dǎo)出功能,需要的可以參考一下2023-07-07