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

C# 標準事件流實例代碼

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

服裝價格變動,觸發(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)文章

最新評論

伊吾县| 贞丰县| 边坝县| 鲜城| 天峨县| 化州市| 清水河县| 满城县| 甘泉县| 宜宾县| 浏阳市| 伊吾县| 晋城| 郑州市| 中江县| 陵水| 安岳县| 军事| 深水埗区| 永新县| 房产| 铜川市| 宁乡县| 浙江省| 定襄县| 格尔木市| SHOW| 东兴市| 利川市| 东阿县| 北辰区| 霞浦县| 盘锦市| 陆良县| 黔江区| 双流县| 皋兰县| 青海省| 甘肃省| 宁化县| 邻水|