C#委托与事件
来源:互联网 发布:淘宝的亚马逊旗舰店 编辑:程序博客网 时间:2024/06/01 08:53
委托的一般使用:
• 实例:把方法当作参数传给另一个方法
1.正确使用1:模板方法,“借用”指定的外部方法来产生结果
1) 相当于“填空题”
2)常位于代码中部
3) 委托有返回值
2.正确使用2:回调(callback)方法,调用指定的外部方法
1)相当于“流水线”
2)常位于代码末尾
3) 委托无返回值
参考代码:
class Program { static void Main(string[] args) { WrapFactory wrapFactory = new WrapFactory(); ProductFactory productFactory = new ProductFactory(); Logger logger = new Logger(); //Func<T,TResult> 和Action<T>是C#内建好的委托,可以直接使用,省去重新声明一个委托的步骤,其中Action<T>没有返回类型 Func<Product> fun1 = new Func<Product>(productFactory.MakeToyCar); Action<Product> action1 = logger.Log; Box box1 = wrapFactory.WrapProduct(fun1,logger.Log); Box box2 = wrapFactory.WrapProduct(productFactory.MakePizza,action1); Console.WriteLine(box1.Product.Name); Console.WriteLine(box2.Product.Name); } } class Logger { public void Log(Product product) { Console.WriteLine("Product:{0} created at {1}.Price is {2}.",product.Name,DateTime.Now,product.Price); } } class Product { public string Name { get; set; } public int Price { get; set; } } class Box { public Product Product { get; set; } } class WrapFactory { /// <summary> /// 委托的一般用法 /// </summary> /// <param name="getProduct">作为模板方法来使用</param> /// <param name="log">作为回调方法来使用</param> /// <returns></returns> public Box WrapProduct(Func<Product> getProduct,Action<Product> log) { Box box = new Box(); Product product = getProduct(); box.Product = product; if (product.Price > 50) { log(product); } return box; } } class ProductFactory { public Product MakePizza() { Product product = new Product(); product.Name = "Pizza"; product.Price = 20; return product; } public Product MakeToyCar() { Product product = new Product(); product.Name = "Toy Car"; product.Price = 200; return product; } }
事件模型的五个组成部分
1. 事件的拥有者
2. 事件成员
3. 事件的响应者
4. 事件处理器
5. 事件订阅
个人理解使用事件的步骤:
1.声明一个委托(如果使用C#内建委托类型,可以省去此步骤)
2.声明一个基于委托的事件
3.引发事件,通常要判断事件是否为null
if (OnOrder != null){ OnOrder(this, menu);}
4.编写用于响应事件的处理函数
5.执行事件订阅
参考代码:
class Connection { //使用C#内建委托Action声明事件 public event Action<string> MessageArrived; private Timer pollTimer; public Connection() { pollTimer = new Timer(100); pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage); } public void Connect() { pollTimer.Start(); } public void Disconnect() { pollTimer.Stop(); } Random random = new Random(); private void CheckForMessage(object source,ElapsedEventArgs e) { Console.WriteLine("Checking for new message."); if (random.Next(9) == 0 && MessageArrived != null) { //引发事件 MessageArrived("Hello Mum!"); } } }
class Display { //用于响应事件的处理函数 public void DisplayMessage(string message) { Console.WriteLine("Message arrived:{0}.", message); } }
class Program { static void Main(string[] args) { Connection connection = new Connection(); Display display = new Display(); //事件的订阅 connection.MessageArrived += new Action<string>(display.DisplayMessage); connection.Connect(); Console.ReadKey(); } }
0 0
- c# 委托与事件
- C#委托与事件
- C# 委托与事件
- c#委托与事件
- C# - 委托与事件
- C#委托与事件
- C# 委托与事件
- C#委托与事件
- c#委托与事件
- C#事件与委托
- C# 事件与委托
- C#事件与委托
- c#委托与事件
- C#委托与事件
- c#委托与事件
- C#委托与事件
- C# 委托与事件
- C#委托与事件
- java设计模式之桥接模式
- 05 JS-DOM之-间接获取节点(即访问关系)
- mysql 行锁 表锁
- Android硬件抽象层(HAL)概要介绍和学习计划
- NO.4 继续HTML之路
- C#委托与事件
- [ZJOI2007]时态同步
- VS2013中附加进程的方式调试IIS接口,设置断点不会命中的问题记录
- 第12周项目3-图遍历算法实现
- OkHttp源码分析
- mysql rand()函数获取随机数
- Leetcode 191 Number of 1 Bits
- 第十四周项目三 是否二叉排序树?
- 动态加载bootstrap表格数据