欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 標(biāo)準(zhǔn)事件流實(shí)例代碼

 更新時(shí)間:2020年07月23日 08:32:37   作者:滑豬小板  
這篇文章主要介紹了C# 標(biāo)準(zhǔn)事件流的實(shí)例代碼,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

服裝價(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)文章

最新評(píng)論