最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年07月23日 08:32:37   作者:滑豬小板  
這篇文章主要介紹了C# 標(biāo)準(zhǔn)事件流的實例代碼,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(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# 標(biāo)準(zhǔn)事件流實例代碼的詳細(xì)內(nèi)容,更多關(guān)于C# 標(biāo)準(zhǔn)事件流的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

扶绥县| 道真| 太湖县| 化州市| 滁州市| 志丹县| 巫山县| 贵南县| 安丘市| 新乡县| 奇台县| 祁门县| 芜湖县| 肥西县| 武川县| 宁河县| 建平县| 历史| 哈密市| 缙云县| 固安县| 收藏| 永登县| 汽车| 东乌| 五大连池市| 稻城县| 当阳市| 三门县| 拜泉县| 盐津县| 吉水县| 徐水县| 石林| 青铜峡市| 鸡泽县| 黄平县| 渝北区| 项城市| 安福县| 长顺县|