当前位置: 首页 > news >正文

【C#补全计划】委托

一、委托的概念

1. 委托使函数(方法)的容器

2. 可以理解为表示函数(方法)的变量类型

3. 用来存储、传递函数(方法)

4. 委托的本质是一个类,用来定义函数(方法)的类型(返回值和参数的类型)

5. 不同的函数(方法)必须对应各自“格式”一致的委托

二、委托的语法

1. 关键字:delegate

2. 语法:访问修饰符 delegate 返回值 委托名(参数列表);

3. 位置:声明在namespace或class中(在namespace中更常用)

4. 不写访问修饰符默认为public,在其他命名空间也能使用。若使用private则不能在其他命名空间使用

三、创建自定义委托

using System;namespace Delegate
{class Program{static void Main(string[] args){}}// 声明一个存储无参无返回值函数的容器:这里只定义了规则,并没有使用public delegate void MyDelegate();// 声明一个存储有参无返回值函数的容器public delegate void MyDelegate2(int a, int b);// 声明一个存储无参有返回值函数的容器public delegate int MyDelegate3();// 声明一个存储有参有返回值函数的容器public delegate int MyDelegate4(int a, int b);
}

四、使用委托

1. 委托常用在:

        (1)作为类的成员

        (2)作为函数的参数

2. 代码

using System;namespace Delegate
{class Program{static void Main(string[] args){// 委托的初始化MyDelegate md1 = new MyDelegate(fun);// 第二种方式MyDelegate md2 = fun;// 委托的使用md1.Invoke();// 第二种方式md2();Console.WriteLine("---------------------");Test t = new Test();t.testFun(fun, atk); // 传递方法}static void fun(){Console.WriteLine("调用fun()");}static void atk(int atk, int def){Console.WriteLine("造成" + (atk - def) + "点伤害");}}// 声明一个存储无参无返回值函数的容器:这里只定义了规则,并没有使用public delegate void MyDelegate();// 声明一个存储有参无返回值函数的容器public delegate void MyDelegate2(int a, int b);// 声明一个存储无参有返回值函数的容器public delegate int MyDelegate3();// 声明一个存储有参有返回值函数的容器public delegate int MyDelegate4(int a, int b);class Test{private MyDelegate fun1;private MyDelegate2 fun2;public void testFun(MyDelegate f1, MyDelegate2 f2){int atk = 3; // 假设这是攻击力int def = 1; // 假设这是防御力// 先处理一些别的逻辑 当这些逻辑处理完了 再执行传入的函数atk *= 2; // 攻击力翻倍def += 2; // 防御力加2f1();f2(atk, def);}}
}

运行结果如下:

五、多播委托

1. 概念:委托变量可以存储多个函数

2. 代码:

using System;namespace Delegate
{class Program{static void Main(string[] args){// 委托的初始化MyDelegate md1 = new MyDelegate(fun);// 第二种方式MyDelegate md2 = fun;// 委托的使用md1.Invoke();// 第二种方式md2();Console.WriteLine("---------------------");Test t = new Test();t.testFun(fun, atk); // 传递方法Console.WriteLine("---------------------");// 多播委托MyDelegate md3 = fun;md3();Console.WriteLine("添加2个函数后:");md3 += fun; // += 向委托md3中添加函数md3 += fun2;md3();Console.WriteLine("删除1个fun()后:");md3 -= fun; // -= 在委托md3中移除函数md3();md3 = null; // 清空委托}static void fun(){Console.WriteLine("调用fun()");}static void fun2(){Console.WriteLine("调用fun2()");}static void atk(int atk, int def){Console.WriteLine("造成" + (atk - def) + "点伤害");}}// 声明一个存储无参无返回值函数的容器:这里只定义了规则,并没有使用public delegate void MyDelegate();// 声明一个存储有参无返回值函数的容器public delegate void MyDelegate2(int a, int b);// 声明一个存储无参有返回值函数的容器public delegate int MyDelegate3();// 声明一个存储有参有返回值函数的容器public delegate int MyDelegate4(int a, int b);class Test{private MyDelegate fun1;private MyDelegate2 fun2;public void testFun(MyDelegate f1, MyDelegate2 f2){int atk = 3; // 假设这是攻击力int def = 1; // 假设这是防御力// 先处理一些别的逻辑 当这些逻辑处理完了 再执行传入的函数atk *= 2; // 攻击力翻倍def += 2; // 防御力加2f1();f2(atk, def);}}
}

运行结果如下:

六、系统定义的委托

1. 前提:引用System命名空间

2. Action:无返回值的委托

3. Func:有返回值的委托

4. 代码:

using System;namespace Delegate
{class Program{static void Main(string[] args){// 委托的初始化MyDelegate md1 = new MyDelegate(fun);// 第二种方式MyDelegate md2 = fun;// 委托的使用md1.Invoke();// 第二种方式md2();Console.WriteLine("---------------------");Test t = new Test();t.testFun(fun, atk); // 传递方法Console.WriteLine("---------------------");// 多播委托MyDelegate md3 = fun;md3();Console.WriteLine("--添加2个函数后:--");md3 += fun; // += 向委托md3中添加函数md3 += fun2;md3();Console.WriteLine("--删除1个fun()后:--");md3 -= fun; // -= 在委托md3中移除函数md3();md3 = null; // 清空委托Console.WriteLine("---------------------");// 使用系统定义的委托// Action:无返回值的委托Action a = fun; // 无参无返回值a();Action<int, int> a2 = atk; // 有参无返回值a2(3, 2);// Func:有返回值的委托Func<int> f1 = fun3; // 无参有返回值int x = f1();Func<int, int, string> f2 = fun4; // 有参有返回值,最后一个泛型为返回值string s = f2(0, 0);}static void fun(){Console.WriteLine("调用fun()");}static void fun2(){Console.WriteLine("调用fun2()");}static int fun3(){Console.WriteLine("调用fun3()");return 0;}static string fun4(int a, int b){Console.WriteLine("调用fun4()");return "";}static void atk(int atk, int def){Console.WriteLine("造成" + (atk - def) + "点伤害");}}// 声明一个存储无参无返回值函数的容器:这里只定义了规则,并没有使用public delegate void MyDelegate();// 声明一个存储有参无返回值函数的容器public delegate void MyDelegate2(int a, int b);// 声明一个存储无参有返回值函数的容器public delegate int MyDelegate3();// 声明一个存储有参有返回值函数的容器public delegate int MyDelegate4(int a, int b);class Test{private MyDelegate fun1;private MyDelegate2 fun2;public void testFun(MyDelegate f1, MyDelegate2 f2){int atk = 3; // 假设这是攻击力int def = 1; // 假设这是防御力// 先处理一些别的逻辑 当这些逻辑处理完了 再执行传入的函数atk *= 2; // 攻击力翻倍def += 2; // 防御力加2f1();f2(atk, def);}}
}

        今天的学习就到这里了。感谢阅读。

        再见!

http://www.dtcms.com/a/333395.html

相关文章:

  • 基于RobustVideoMatting(RVM)进行视频人像分割(torch、onnx版本)
  • 【opencv-Python学习笔记(5):几何变换】
  • 补充日志之-配置文件解析指南(Centos7)
  • 容器内部再运行Docker(DinD和DooD)
  • CUDA中的基本概念
  • Linux软件编程:进程线程(线程)
  • 结构体(Struct)、枚举(Enum)的使用
  • 基于SpringBoot的房产销售系统
  • 护栏卫士碰撞报警系统如何实时监测护栏的状态
  • 系统时钟配置
  • 38 C++ STL模板库7-迭代器
  • 用ICO图标拼成汉字
  • BFS和codetop复习
  • 复杂度扫尾+链表经典算法题
  • Klipper-probe模块
  • H5449G降压恒流无人机照明驱动芯片方案24V/36V/48V/72V降6V12V9V /8A替换NCL30160
  • 探索无人机图传技术:创新视野与无限可能
  • C#WPF实战出真汁06--【系统设置】--餐桌类型设置
  • Linux 系统中, LANG 和 LC_ALL变量有什么区别与联系?
  • 文档对比(java-diff-utils)
  • lidar2imu/auto_caliban以及manual_calib安装过程
  • 8.15网络编程——UDP和TCP并发服务器
  • qs是什么?
  • Python入门第3课:Python中的条件判断与循环语句
  • Ubuntu20.04下Remmina的VNC密码忘记后重置
  • 手机场景性能测试中的部分关键指标
  • Layui 语法详解与全功能示例
  • iOS 性能监控实战,多工具协作完成全方位分析
  • LCR 076. 数组中的第 K 个最大元素
  • 代码随想录刷题Day33