马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#基础之委托

[复制链接]
查看1971 | 回复0 | 2019-9-26 09:21:42 | 显示全部楼层 |阅读模式

通过以下头脑导图,学习委托的根本概念,背面着重讲授委托的运用,盼望通过最简单的方式劳绩更多的知识。

092206ugz8cvccnz8ca5eg.png

1.委托的各种写法

1、委托 委托名=new 委托(会调用的方法名); 委托名(参数);

2、委托 委托名 =会调用的方法名; 委托名(参数);

3、匿名方法:委托 委托名=delegate(参数){会调用的方法体};委托名(参数);

4、拉姆达表达式:委托 委托名=((参数1,。。参数n)=>{会调用的方法体});委托名(参数);

5、用Action和Func

Action<参数1, 参数2,> 委托名= ((参数1,参数2) => {不带返回值的方法体 });委托名(参数1,参数2);

Func<参数1, 参数2, 返回值> 委托名= ((参数1,参数2) => {带返回值的方法体 });返回值=委托名(参数1,参数2);

示例:

  1. public delegate int Call(int num1, int num2);
  2. class SimpleMath
  3. {
  4. // 乘法方法
  5. public static int Multiply(int num1, int num2)
  6. {
  7. return num1 * num2;
  8. }
  9. // 除法方法
  10. public int Divide(int num1, int num2)
  11. {
  12. return num1 / num2;
  13. }
  14. }
  15. class Test
  16. {
  17. static void Main(string[] args)
  18. {
  19. //--------------------第一种写法------------------------//
  20. Call objCall = new Call(SimpleMath.Multiply);
  21. Call objCall1 = new Call(new SimpleMath().Divide);
  22. //--------------------第二种写法------------------------//
  23. Call objCall = SimpleMath.Multiply;
  24. Call objCall1 = new SimpleMath().Divide;
  25. //--------------------第三种写法------------------------//
  26. Call objCall = delegate(int a, int b)
  27. {
  28. return a * b;
  29. };
  30. Call objCall1 = delegate(int a, int b)
  31. {
  32. return a / b;
  33. };
  34. //--------------------第四种写法------------------------//
  35. Call objCall =((int a,int b)=> { return a*b;});
  36. Call objCall1 = ((int a, int b) => { return a / b; });
  37. //--------------------第五种写法------------------------//
  38. Func<int, int, int> objCall = ((a, b) => { return a * b; });
  39. Func<int, int, int> objCall1 = ((a, b) => { return a / b; });
  40. Action<int, int> ob = ((a, b) => { Console.WriteLine(a * b); });
  41. ob(5, 3);
  42. //----------------------------------------------------//
  43. int result = objCall(5, 3);
  44. int result1 = objCall1(5, 3);
  45. System.Console.WriteLine("结果1为 {0},结果2为{1}", result,result1);
  46. Console.ReadKey();
  47. }
  48. }
复制代码

2.委托的运用

委托的运用记着两点:

1.将方法看成参数实例化委托对象;

2.将方法的参数通报给委托对象,以实实际际的方法调用。

委托常用场景:

1.模板方法:

如以下定义类CalculateFactory,用于定义各种计算方法,然后通过Calculate方法袒露出来给外界使用,而Calculate方法通过传入委托对象new Calculate(x1.Add)来实现对Add方法的调用。这是委托模板方法使用较简单的一种形式,它还可以有许多变种。

下面这段程序不消委托完全可以实现同样的逻辑,为什么要“故弄玄虚”呢?因为示例是为了阐明委托作为模板方法的用法,故而用了最简单的一种,实际运用过程中,通常与计划模式相团结,以实现代码的高复用低耦合。进一步延伸,实际计划模式中也较少用委托,而用接口、抽象类来实现“模板方法”的功能,具体要怎么用是看个人风俗和便捷程度。委托用的最多的场景是下面要先容的回调方法。

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. CalculateFactory x1 = new CalculateFactory();
  6. CalculateFactory x2 = new CalculateFactory();
  7. x1.Calculate(10, 9, new Calculate(x1.Add));
  8. x2.Calculate(10, 9, new Calculate(x2.Reduce));
  9. Console.ReadKey();
  10. }
  11. }
  12. public delegate void Calculate(int a, int b);
  13. public class CalculateFactory
  14. {
  15. public void Calculate(int a, int b, Calculate calculateDelegae)
  16. {
  17. calculateDelegae(a, b);
  18. }
  19. public void Add(int a, int b)
  20. {
  21. Console.WriteLine(string.Format("This is a+b={0}", a + b));
  22. }
  23. public void Reduce(int a, int b)
  24. {
  25. Console.WriteLine(string.Format("This is a-b={0}", a - b));
  26. }
  27. }
复制代码

2.回调方法:

回调方法与模板方法并不是并列的两种范例,其本质都是一样的,即将方法当成参数通报并调用,是通过应用场景来分类的。主调方法(调用回调方法的方法体)在满足某种条件或完成某种逻辑后去调用的方法,称为回调方法。将上面示例改造成含有回调方法的程序。

示例:这里既用到了模板方法,也用到了回调方法。示例代码泉源于刘铁猛大师的示例,在此表示感谢。

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. ProductFactory productFactory = new ProductFactory();
  6. WrapFactory wrapFactory = new WrapFactory();
  7. Func<Product> func1 = new Func<Product>(productFactory.MakePizza);
  8. Func<Product> func2 = new Func<Product>(productFactory.MakeToyCar);
  9. Logger logger = new Logger();
  10. Action<Product> log = new Action<Product>(logger.Log); //Log的委托;
  11. Box box1 = wrapFactory.WrapProduct(func1, log);
  12. Box box2 = wrapFactory.WrapProduct(func2, log);
  13. Console.WriteLine(box1.Product.Name);
  14. }
  15. class Product //产物类
  16. {
  17. public string Name { get; set; }
  18. public double Price { get; set; }
  19. }
  20. class Box //盒子类
  21. {
  22. public Product Product { get; set; }
  23. }
  24. class Logger
  25. {
  26. public void Log(Product product)
  27. {
  28. Console.WriteLine(product.Price);
  29. }
  30. }
  31. class WrapFactory //包装工厂
  32. {
  33. public Box WrapProduct(Func<Product> getProduct, Action<Product> logCallback)
  34. {
  35. Box box = new Box();
  36. Product product = getProduct.Invoke();//此处使用的是间接的同步调用,如果使用间接异步调用用BeginInvoke();
  37. if (product.Price > 50) //如果产物代价大于50,就执行回调方法;
  38. {
  39. logCallback(product);
  40. }
  41. box.Product = product;
  42. return box;
  43. }
  44. }
  45. class ProductFactory //产物工厂
  46. {
  47. public Product MakePizza()
  48. {
  49. Product product = new Product();
  50. product.Name = "Pizza";
  51. product.Price = 30;
  52. return product;
  53. }
  54. public Product MakeToyCar()
  55. {
  56. Product product = new Product();
  57. product.Name = "ToyCar";
  58. product.Price = 100;
  59. return product;
  60. }
  61. }
  62. }
复制代码

3.总结

委托底子内容根本就是这些,回调方法在实际使用中也是最多的,上面回调方法的示例是有实用代价的,必要好好了解。委托尚有多播委托等进阶应用,在此不作先容,但必要相识其概念,以便碰到相应场景时翻翻资料能找到办理方案。







来源:https://www.cnblogs.com/qcst123/archive/2019/09/24/11575557.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则