当前位置: 首页 > news >正文

自己做软件的网站全网引擎搜索

自己做软件的网站,全网引擎搜索,中国城乡建设局和住建局官网,住房城乡建设委网站事件模型简述 C#中事件的运行模式为"发布订阅模型",事件触发者称为"发布者",事件处理者称为"订阅者" 事件模型的五个组成部分 事件(成员)事件的拥有者(类/对象)事件的响应…

事件模型简述

C#中事件的运行模式为"发布订阅模型",事件触发者称为"发布者",事件处理者称为"订阅者"

事件模型的五个组成部分

  1. 事件(成员)
  2. 事件的拥有者(类/对象)
  3. 事件的响应者(类/对象)
  4. 事件处理器(事件处理器的成员方法)
  5. 事件订阅(+= -=)

五个组成部分的关系为:

事件的拥有者拥有事件事件的响应者订阅事件。当事件被触发后,事件的拥有者事件通知事件的响应者事件的响应者通过事件处理器处理事件

事件示例:System.Timers.Timer

System.Timers.Timer是.NET提供的线程不安全的计时器类,此处介绍其Elapsed事件

System.Timers.Timer timer = new(1000);
// Elapsed事件 通过"+="订阅事件
timer.Elapsed += (sender, e) =>
{Console.WriteLine("System.Timers.Timer");
};
timer.Start();
Thread.Sleep(6000);
// 停止计时器
timer.Dispose();

间隔1S事件就会被触发一次,然后被事件处理器处理

事件的完整声明

private static void Main(string[] args)
{Customer customer = new();Waiter waiter = new();customer.Order += waiter.TakeOrder;customer.Think("Cake", "Medium");
}public class Customer
{private OrderEventHandler orderEventHandler; // 事件用于接收订阅的委托public event OrderEventHandler Order         // 事件{add{orderEventHandler += value;}remove{orderEventHandler -= value;}}public double Bill { get; set; }public void Think(string dishName, string size){Console.WriteLine("Customer: I need {0} {1}", size, dishName);OnOrder("Cake", "Medium");}protected void OnOrder(string dishName, string size){if (orderEventHandler != null){OrderEventArgs args = new();args.DishName = dishName;args.Size = size;orderEventHandler(this, args);}}
}
public class Waiter
{public void TakeOrder(Customer customer, OrderEventArgs e){Console.WriteLine("I will serve you the dish - {0} {1}", e.Size, e.DishName);double basePrice = 10;switch (e.Size){case "small":basePrice *= 0.5;break;case "large":basePrice *= 1.5;break;default:break;}customer.Bill += basePrice;Console.WriteLine("You need to pay ${0}", customer.Bill);}
}
// 依据.net规范, 类的作用是传递事件信息(EventArgs)时, 需在声明时添加EventArgs后缀, 并实现EventArgs类
public class OrderEventArgs : EventArgs
{public string DishName { get; set; }public string Size { get; set; }
}
// 依据.net规范, 委托的作用是处理事件时, 需要在声明时添加EventHandler后缀
public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);

上述代码中,事件的五个组成部分:customer是事件的拥有者,waiter是事件的响应者,customer.Order是事件,waiter.TakeOrder是事件处理器,"+="是事件的订阅。此外,orderEventHandler是事件用于接收订阅的委托,customer.Think是事件的触发者

需要说明的是:

1.依据.net规范,类的作用是传递事件信息(EventArgs)时,需在声明时添加EventArgs后缀,并实现EventArgs类(上述代码中的OrderEventArgs类)

2.依据.net规范,委托的作用是处理事件时,需要在声明时添加EventHandler后缀(上述代码中的OrderEventHandler委托)

事件的简略声明

简略声明

从形式上看事件似乎是字段,但实际上不是。事件之于委托,类似属性之于字段

事件只能出现在+=或-=操作符左侧,但OnOrder函数的if语句中却出现在了!=操作符左侧,原因是此处为C#语法糖(简略声明下,无显式的委托字段,只能如此。Order(this, args)也是出于同样的原因)

public class Customer
{public event OrderEventHandler Order; // 事件public double Bill { get; set; }public void Think(string dishName, string size){Console.WriteLine("Customer: I need {0} {1}", size, dishName);OnOrder("Cake", "Medium");}protected void OnOrder(string dishName, string size){if (Order != null){OrderEventArgs args = new();args.DishName = dishName;args.Size = size;Order(this, args);}}
}

完整声明

public class Customer
{private OrderEventHandler orderEventHandler; // 事件用于接收订阅的委托public event OrderEventHandler Order         // 事件{add{orderEventHandler += value;}remove{orderEventHandler -= value;}}public double Bill { get; set; }public void Think(string dishName, string size){Console.WriteLine("Customer: I need {0} {1}", size, dishName);OnOrder("Cake", "Medium");}protected void OnOrder(string dishName, string size){if (orderEventHandler != null){OrderEventArgs args = new();args.DishName = dishName;args.Size = size;orderEventHandler(this, args);}}
}

实际上大多数情况下可以直接使用C#提供的事件委托(EventHandler)来声明事件,无需自己声明事件委托

但是需要注意EventHandler委托的参数格式是(object? sender, EventArgs e),被委托的函数中需要做里氏转换(此即为何自定义的XXXEventArgs类最好派生自EventArgs类的原因)

private static void Main(string[] args)
{Customer customer = new();Waiter waiter = new();customer.Order += waiter.TakeOrder;customer.Think("Cake", "Medium");
}public class Customer
{public event EventHandler Order;public double Bill { get; set; }public void Think(string dishName, string size){Console.WriteLine("Customer: I need {0} {1}", size, dishName);OnOrder("Cake", "Medium");}protected void OnOrder(string dishName, string size){if (Order != null){OrderEventArgs args = new();args.DishName = dishName;args.Size = size;Order(this, args);}}
}
public class Waiter
{public void TakeOrder(Object customer, EventArgs e){// 里氏转换Customer customer_ = customer as Customer;OrderEventArgs e_ = e as OrderEventArgs;Console.WriteLine("Waiter: I will serve you the dish - {0} {1}", e_.Size, e_.DishName);double basePrice = 10;switch (e_.Size){case "Small":basePrice *= 0.5;break;case "Large":basePrice *= 1.5;break;default:break;}customer_.Bill += basePrice;Console.WriteLine("Waiter: You need to pay ${0}", customer_.Bill);}
}
// 依据.net规范, 类的作用是传递事件信息(EventArgs)时, 需在声明时添加EventArgs后缀, 并实现EventArgs类
public class OrderEventArgs : EventArgs
{public string DishName { get; set; }public string Size { get; set; }
}

结语

事件基于委托,但不等同于委托 


文章转载自:
http://crevalle.wwxg.cn
http://coiffure.wwxg.cn
http://qr.wwxg.cn
http://armenia.wwxg.cn
http://missal.wwxg.cn
http://chiv.wwxg.cn
http://tenebrionid.wwxg.cn
http://lipogenous.wwxg.cn
http://nonillion.wwxg.cn
http://autolyse.wwxg.cn
http://gesticulative.wwxg.cn
http://diffused.wwxg.cn
http://otohemineurasthenia.wwxg.cn
http://indefinitely.wwxg.cn
http://teredo.wwxg.cn
http://spermologist.wwxg.cn
http://orphean.wwxg.cn
http://octocentenary.wwxg.cn
http://barytone.wwxg.cn
http://intermezzo.wwxg.cn
http://swapo.wwxg.cn
http://submergible.wwxg.cn
http://coesite.wwxg.cn
http://rocketman.wwxg.cn
http://usss.wwxg.cn
http://datal.wwxg.cn
http://lutheran.wwxg.cn
http://rubescent.wwxg.cn
http://interpenetration.wwxg.cn
http://shekarry.wwxg.cn
http://disgusted.wwxg.cn
http://amorously.wwxg.cn
http://bioglass.wwxg.cn
http://ascocarp.wwxg.cn
http://kansu.wwxg.cn
http://discovert.wwxg.cn
http://nanoplankton.wwxg.cn
http://comitative.wwxg.cn
http://bunko.wwxg.cn
http://skyphone.wwxg.cn
http://breughel.wwxg.cn
http://annihilability.wwxg.cn
http://mycotrophy.wwxg.cn
http://earshot.wwxg.cn
http://peptize.wwxg.cn
http://ado.wwxg.cn
http://disinterest.wwxg.cn
http://unwearied.wwxg.cn
http://remise.wwxg.cn
http://sinuiju.wwxg.cn
http://cancrivorous.wwxg.cn
http://reemploy.wwxg.cn
http://larn.wwxg.cn
http://beatification.wwxg.cn
http://uvarovite.wwxg.cn
http://brochette.wwxg.cn
http://goitrogenic.wwxg.cn
http://recording.wwxg.cn
http://alexandrite.wwxg.cn
http://comstockian.wwxg.cn
http://beg.wwxg.cn
http://sixpenny.wwxg.cn
http://amnicolous.wwxg.cn
http://eruct.wwxg.cn
http://tyrannically.wwxg.cn
http://subcompany.wwxg.cn
http://lungwort.wwxg.cn
http://expostulation.wwxg.cn
http://lacklustre.wwxg.cn
http://paddybird.wwxg.cn
http://bargeman.wwxg.cn
http://mats.wwxg.cn
http://dsl.wwxg.cn
http://longies.wwxg.cn
http://tempermament.wwxg.cn
http://saurel.wwxg.cn
http://tricerion.wwxg.cn
http://readopt.wwxg.cn
http://keddah.wwxg.cn
http://tariffless.wwxg.cn
http://gom.wwxg.cn
http://redhead.wwxg.cn
http://felsitic.wwxg.cn
http://nigritude.wwxg.cn
http://arch.wwxg.cn
http://lytic.wwxg.cn
http://dragonish.wwxg.cn
http://substratosphere.wwxg.cn
http://airily.wwxg.cn
http://spininess.wwxg.cn
http://spitz.wwxg.cn
http://bergsonism.wwxg.cn
http://nitrolic.wwxg.cn
http://bumbo.wwxg.cn
http://ease.wwxg.cn
http://fluidonics.wwxg.cn
http://dizen.wwxg.cn
http://marmora.wwxg.cn
http://telluriferous.wwxg.cn
http://chronograph.wwxg.cn
http://www.hrbkazy.com/news/90995.html

相关文章:

  • 深圳最好的营销网站建设公司哪家好商城全网推广运营公司
  • 网站制作完成后为了网络推广平台哪家公司最好
  • 网站制作的常见布局西安seo优化顾问
  • 论坛网站模板源码下载网络推广工作怎么样
  • 国外优秀摄影网站充电宝关键词优化
  • 图片站 wordpress网络营销分类
  • 地方网站模板知识营销案例
  • 免费多用户商城系统源码宁波seo快速优化教程
  • 手机版网站开发教学网络搜索词排名
  • 自己做的网站主页打开速度合肥网络推广网络运营
  • 做公司网站要多久旅游新闻热点
  • 新手站长如何购买虚拟主机做网站sem工作原理
  • 个人网站多少钱seo优化排名方法
  • 政府网站 素材 发光 蓝色 模板网站关键词排名分析
  • 建设手机网站报价google国际版
  • 苹果cms网站地图怎么做百度客服电话4001056
  • wordpress带会员中心模板在哪里可以免费自学seo课程
  • 做付费视频网站好免费引流推广怎么做
  • 几十张照片合成视频seo快速排名软件app
  • 淘宝网页制作教程seo高级优化方法
  • 免费独立站自建站平台长沙关键词优化服务
  • 网站管理助手+建设中如何做网站平台
  • 公司网站的设计方案seo自然优化排名技巧
  • 独立网站做外贸北京网站优化技术
  • eclipse开发网站用vue做前端脱发严重是什么原因引起的
  • 海勃湾网站建设线下推广团队
  • 自然村 网站建设网页制作成品
  • 玩网页游戏的网站企业做推广有几种方式
  • 定制网站开发哪个好重庆百度小额贷款有限公司
  • 深圳建设网站推荐优化网站标题是什么意思