zoukankan      html  css  js  c++  java
  • 委托是什么?

    委托是什么?

    • 委托(delegate)是函数指针的升级版。

    学过C/C++的小伙伴都知道函数指针,函数指针就是指向函数的指针。

    include<studio.h>
    
    int (*Calc)(int a,int b);//声明一个返回int类型的函数指针类型。
    
    int Add (int a,int b)
    {
            return a+b;
    }
    
    int Sub(int a,int b)
    {
            return a-b;
    }
    
    int main()
    {
        int x = 100;
        int y = 50;
        int z = 0;
    
         Calc funcPoint1 = &Add;
         Calc funcPoint2 = &Sub;
    
        z = funcPoint1(x,y);
        printf("%d+%d=%d
    ",x,y,z);
    
        z = funcPoint2(x,y);
        printf("%d-%d=%d
    ",x,y,z);
    
        system("pause");
        return 0;
    }

    这里提一下java没有函数指针的概念。Java 语言由 C++ 发展而来,为了提高应用安全性,Java 语言禁止程序员直接访问内存地址。即 Java 语言把 C++ 中所有与指针相关的内容都舍弃掉了。

    • 委托的简单使用

    通常情况下我们直接使用C#内置的委托实例更简单。Action Func 是 C# 内置的委托实例,它们都有很多重载以方便使用。

    class Program
    {
        static void Main(string[] args)
        {
            var calculator = new Calculator();
            // Action 用于无形参无返回值的方法。
            Action action = new Action(calculator.Report);
            calculator.Report();
            action.Invoke();
            // 模仿函数指针的简略写法。
            action();
    
            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 = 200;
            int z = 0;
    
            z = func1.Invoke(x, y);
            Console.WriteLine(z);
            z = func2.Invoke(x, y);
            Console.WriteLine(z);
    
            // Func 也有简略写法。
            z = func1(x, y);
            Console.WriteLine(z);
            z = func2(x, y);
            Console.WriteLine(z);
        }
    }
    
    class Calculator
    {
        public void Report()
        {
            Console.WriteLine("I have 3 methods.");
        }
    
        public int Add(int a, int b)
        {
            return a + b;
        }
    
        public int Sub(int a, int b)
        {
            return a - b;
        }
    }

    委托的声明

    委托是一种类:

    static void Main(string[] args)
    {
        Type t = typeof(Action);
        Console.WriteLine(t.IsClass);
    }

    //console:true;

    委托是类,所以声明位置是和 class 处于同一个级别。但 C# 允许嵌套声明类(一个类里面可以声明另一个类),所以有时也会有 delegate 在 class 内部声明的情况。

    public delegate double Calc(double x, double y);
    
    class Program
    {
        static void Main(string[] args)
        {
            var calculator = new Calculator();
            var calc1 = new Calc(calculator.Mul);
    
            Console.WriteLine(calc1(5, 6));
        }
    }
    
    class Calculator
    {
        public double Mul(double x, double y)
        {
            return x * y;
        }
    
        public double Div(double x, double y)
        {
            return x / y;
        }
    }
  • 相关阅读:
    split 过滤空的元素
    Python内置函数(65)——staticmethod
    Python内置函数(64)——classmethod
    Python内置函数(63)——property
    Python内置函数(62)——exec
    Python内置函数(61)——eval
    Js里头的对象字面量
    Js中的prototype的用法二
    Js中的prototype的用法一
    Mxgraph使用总结二
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/12120838.html
Copyright © 2011-2022 走看看