C#(11):委托详解

  1. 什么是委托
    是函数指针的升级版,通过委托保留函数指针对应的操作

    委托定义
    namespace DelegateExample_point
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Calculator calculator = new Calculator();
                
                //只需要Calculator类的方法名,Action类型委托
                Action action = new Action(calculator.Report);
                //直接调用方法
                calculator.Report();
                //间接调用,Invoke调用
                action.Invoke();
                //等效于间接调用,模仿函数指针的方法
                action();
                /**********/
                //定义Func类型委托
                Func<int,int,int>func1=new Func<int, int, int>(calculator.Add);
                Func<int,int,int>func2 = new Func<int,int,int>(calculator.Sub);
                int x = 100;
                int y = 100;
                int z = 0;
                z = func1.Invoke(x, y);
                //使用函数指针式的写法
                z=func1(x, y);
                Console.WriteLine(z);
                z = func2.Invoke(x, y);
                //使用函数指针式的写法
                z = func2(x, y);
                Console.WriteLine(z);
            }
        }
        class Calculator
        {
            public void Report()
            {
                Console.WriteLine("3 methods");
            }
            public int Add(int a,int b)
            {
                return a + b;
            }
            public int Sub(int a,int b)
            {
                return a - b;
            }
        }
    }
  2. 自己声明委托,
    声明时在命名空间下一层进行声明,和其它类同级时使用public方法,也可以使用嵌套类,这时实例化委托对象时,委托类型前面要加类名.


    namespace DelegateExample_point
    {
        //声明委托类型
        public delegate int Cal(int x,int y);
        internal class Program
        {
            static void Main(string[] args)
            {
                Calculator calculator = new Calculator();
                
                //只需要Calculator类的方法名,Action类型委托
                Action action = new Action(calculator.Report);
                //直接调用方法
                calculator.Report();
                //间接调用,Invoke调用
                action.Invoke();
                //等效于间接调用,模仿函数指针的方法
                action();
                /**************************************************************************************************************/
                //定义Func类型委托
                Func<int,int,int>func1=new Func<int, int, int>(calculator.Add);
                Func<int,int,int>func2 = new Func<int,int,int>(calculator.Sub);
                int x = 100;
                int y = 100;
                int z = 0;
                z = func1.Invoke(x, y);
                //使用函数指针式的写法
                z=func1(x, y);
                Console.WriteLine(z);
                z = func2.Invoke(x, y);
                //使用函数指针式的写法
                z = func2(x, y);
                Console.WriteLine(z);
                /**************************************************************************************************************/
                //引用自定义的委托类型
                Cal cal1=new Cal(calculator.Add);
                Cal cal2=new Cal(calculator.Sub);
                Console.WriteLine(cal1(1, 2));
                Console.WriteLine(cal2(1,2));
            }
        }
        class Calculator
        {
            public void Report()
            {
                Console.WriteLine("3 methods");
            }
            public int Add(int a,int b)
            {
                return a + b;
            }
            public int Sub(int a,int b)
            {
                return a - b;
            }
        }
    }


  3. 模板方法:
    回调方法:
    class WrapFactory 
    {/// <summary>
    /// 模板方法,接收一个委托类型Func的参数 Product类型并命名为getProduct,不用重复写打包方法,只接受传进来的委托参数(包装好的函数当作参数传递),能有效提高代码的可重用性
    /// 回调方法,调用一个无返回值的Action类型
    /// </summary>
    /// <returns></returns>
        public Box WrapProduct(Func<Product> getProduct,Action<Product> logCallback)
        {
            Box box = new Box();
            //间接调用func3,即MakeToyCar方法
            Product product = getProduct.Invoke();
            box.Product = product;
            //
            if(product.Price >= 50)
            {
                logCallback(product);
            }
            return box;
        }
    }

posted @ 2024-02-02 15:23  HY10-SALTEST-FISH  阅读(3)  评论(0编辑  收藏  举报