C# 标准事件流实例代码

所属分类: 软件编程 / C#教程 阅读数: 151
收藏 0 赞 0 分享

服装价格变动,触发淘宝发布活动和消费者购买衣服事件流

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>
    /// 衣服价格事件参数 一般会为特定的事件去封装个参数类型
    /// </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($"淘宝:发布衣服价格下降的公告,{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},现在价格{args.NewPrice},果断买了!");
        else
          Console.WriteLine($"消费者:等等看,降价了再说");
      }
    }

    public static void Show()
    {
      Clothes clothes = new Clothes()
      {
        Id = "12111-XK",
        Name = "优衣库",
        Price = 128
      };
      //订阅:把订户和发布者的事件关联起来
      clothes.PriceRiseHandler += new TaoBao().PublishPriceInfo;
      clothes.PriceRiseHandler += new Consumer().Buy;
      //价格变化,自动触发订户订阅的事件
      clothes.Price = 300;
    }

  }

调用:

clothes.Price = 300; 
EventStandard.Show();

clothes.Price = 98; 
EventStandard.Show();

以上就是C# 标准事件流实例代码的详细内容,更多关于C# 标准事件流的资料请关注脚本之家其它相关文章!

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多