noopenqq,igotya,巫婆变阿sue中文版
最近一直在研究委托与事件,下面是我的个人理解
1 class BoliEventAgrs : EventArgs//继承系统事件参数接口 2 { 3 public readonly int tem; 4 public BoliEventAgrs(int tem) 5 { 6 this.tem = tem; 7 } 8 }
//注册 boli.BoliWaterEvent += boli1.Alarm; boli.BoliWaterEvent += boli1.Alarm1;
//执行
boli.Boliwater();
4。效果
5.下面是完整代码
using System; namespace DelegateEventObserver { class Program { static void Main(string[] args) { a boli = new a(); b boli1 = new b(); boli.BoliWaterEvent += boli1.Alarm; boli.BoliWaterEvent += boli1.Alarm1; boli.Boliwater(); Console.ReadKey(); } } class BoliEventAgrs : EventArgs//继承系统事件参数接口 { public readonly int tem; public BoliEventAgrs(int tem) { this.tem = tem; } } class a { //public delegate void Boli(object sender,BoliEventAgrs e); public event EventHandler<BoliEventAgrs> BoliWaterEvent;//BoliEventAgrs 为委托参数 public void Boliwater() { for (int i=0; i<100;i++) { if (i>90) { BoliEventAgrs e = new BoliEventAgrs(i); if (BoliWaterEvent != null) { BoliWaterEvent(this, e); } } } } } /// <summary> /// 委托方法,事件注册方法,参数e为 控制温度 /// </summary> class b { public void Alarm(object sender,BoliEventAgrs e) { if (e.tem>95) { Console.WriteLine("水温{0}",e.tem); } } public void Alarm1(object sender, BoliEventAgrs e) { if (e.tem >98) { Console.WriteLine("水温{0},马上开啦", e.tem); } } } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论