当前位置: 移动技术网 > IT编程>开发语言>c# > C#基础之委托

C#基础之委托

2019年09月25日  | 移动技术网IT编程  | 我要评论

  通过以下思维导图,学习委托的基本概念,后面着重讲解委托的运用,希望通过最简单的方式收获更多的知识。

1.委托的各种写法

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

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

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

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

5、用action<t>和func<t>

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; });
        
        //--------------------第五种写法------------------------//
        func<int, int, int> objcall = ((a, b) => { return a * b; });
        func<int, int, int> objcall1 = ((a, b) => { return a / b; });
        action<int, 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 a+b={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();

            func<product> func1 = new func<product>(productfactory.makepizza);
            func<product> func2 = new func<product>(productfactory.maketoycar);

            logger logger = new logger();
            action<product> log = new action<product>(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(func<product> getproduct, action<product> 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;
            }
        }
    }

 

3.总结

   委托基础内容基本就是这些,回调方法在实际使用中也是最多的,上面回调方法的示例是有实用价值的,需要好好体会。委托还有多播委托等进阶应用,在此不作介绍,但需要了解其概念,以便碰到相应场景时翻翻资料能找到解决方案。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网