网站建设项目建议书,wordpress 免费电商主题,网页设计素材制作,page文件怎么转换wordpress通过以下思维导图#xff0c;学习委托的基本概念#xff0c;后面着重讲解委托的运用#xff0c;希望通过最简单的方式收获更多的知识。 1.委托的各种写法
1、委托 委托名new 委托#xff08;会调用的方法名); 委托名#xff08;参数#xff09;;
2、委托 委托名 会调用…通过以下思维导图学习委托的基本概念后面着重讲解委托的运用希望通过最简单的方式收获更多的知识。 1.委托的各种写法
1、委托 委托名new 委托会调用的方法名); 委托名参数;
2、委托 委托名 会调用的方法名; 委托名参数
3、匿名方法委托 委托名delegate(参数{会调用的方法体};委托名参数
4、拉姆达表达式委托 委托名参数1。。参数n{会调用的方法体}委托名参数
5、用ActionT和FuncT
Action参数1, 参数2, 委托名 ((参数1参数2) {不带返回值的方法体 });委托名参数1参数2
Func参数1, 参数2, 返回值 委托名 ((参数1参数2) {带返回值的方法体 });返回值委托名参数1参数2
示例
public delegate int Call(int num1, int num2);
class SimpleMath
{// 乘法方法public static int Multiply(int num1, int num2){return num1 * num2;}// 除法方法public int Divide(int num1, int num2){return num1 / num2;}
}class Test
{static void Main(string[] args){//--------------------第一种写法------------------------//Call objCall new Call(SimpleMath.Multiply);Call objCall1 new Call(new SimpleMath().Divide);//--------------------第二种写法------------------------//Call objCall SimpleMath.Multiply;Call objCall1 new SimpleMath().Divide;//--------------------第三种写法------------------------//Call objCall delegate(int a, int b){return a * b;};Call objCall1 delegate(int a, int b){return a / b;};//--------------------第四种写法------------------------//Call objCall ((int a,int b) { return a*b;});Call objCall1 ((int a, int b) { return a / b; });//--------------------第五种写法------------------------//Funcint, int, int objCall ((a, b) { return a * b; });Funcint, int, int objCall1 ((a, b) { return a / b; });Actionint, int ob ((a, b) { Console.WriteLine(a * b); });ob(5, 3);//----------------------------------------------------//int result objCall(5, 3);int result1 objCall1(5, 3);System.Console.WriteLine(结果1为 {0},结果2为{1}, result,result1);Console.ReadKey();}
}
2.委托的运用
委托的运用记住两点
1.将方法当作参数实例化委托对象 2.将方法的参数传递给委托对象以实现实际的方法调用。
委托常用场景
1.模板方法 如以下定义类CalculateFactory用于定义各种计算方法然后通过Calculate方法暴露出来给外界使用而Calculate方法通过传入委托对象new Calculate(x1.Add)来实现对Add方法的调用。这是委托模板方法使用较简单的一种形式它还可以有很多变种。 下面这段程序不用委托完全可以实现同样的逻辑为什么要“故弄玄虚”呢因为示例是为了说明委托作为模板方法的用法故而用了最简单的一种实际运用过程中通常与设计模式相结合以实现代码的高复用低耦合。进一步延伸实际设计模式中也较少用委托而用接口、抽象类来实现“模板方法”的功能具体要怎么用是看个人习惯和便捷程度。委托用的最多的场景是下面要介绍的回调方法。
class Program{static void Main(string[] args){CalculateFactory x1 new CalculateFactory();CalculateFactory x2 new CalculateFactory();x1.Calculate(10, 9, new Calculate(x1.Add));x2.Calculate(10, 9, new Calculate(x2.Reduce));Console.ReadKey();}}public delegate void Calculate(int a, int b);public class CalculateFactory{public void Calculate(int a, int b, Calculate calculateDelegae){calculateDelegae(a, b);}public void Add(int a, int b){Console.WriteLine(string.Format(This is ab{0}, a b));}public void Reduce(int a, int b){Console.WriteLine(string.Format(This is a-b{0}, a - b));}}
2.回调方法 回调方法与模板方法并不是并列的两种类型其本质都是一样的即将方法当成参数传递并调用是通过应用场景来分类的。主调方法调用回调方法的方法体在满足某种条件或完成某种逻辑后去调用的方法称为回调方法。将上面示例改造成含有回调方法的程序。
class Program{static void Main(string[] args){ProductFactory productFactory new ProductFactory();WrapFactory wrapFactory new WrapFactory();FuncProduct func1 new FuncProduct(productFactory.MakePizza);FuncProduct func2 new FuncProduct(productFactory.MakeToyCar);Logger logger new Logger();ActionProduct log new ActionProduct(logger.Log); //Log的委托;Box box1 wrapFactory.WrapProduct(func1, log);Box box2 wrapFactory.WrapProduct(func2, log);Console.WriteLine(box1.Product.Name);}class Product //产品类{public string Name { get; set; }public double Price { get; set; }}class Box //盒子类{public Product Product { get; set; }}class Logger{public void Log(Product product){Console.WriteLine(product.Price);}}class WrapFactory //包装工厂{public Box WrapProduct(FuncProduct getProduct, ActionProduct logCallback){Box box new Box();Product product getProduct.Invoke();//此处使用的是间接的同步调用如果使用间接异步调用用BeginInvoke();if (product.Price 50) //如果产品价格大于50就执行回调方法;{logCallback(product);}box.Product product;return box;}}class ProductFactory //产品工厂{public Product MakePizza(){Product product new Product();product.Name Pizza;product.Price 30;return product;}public Product MakeToyCar(){Product product new Product();product.Name ToyCar;product.Price 100;return product;}}}