C#教程https://www.jxasp.com/blog
委托的定义以及如何理解委托#
委托现实的定义:
本人不需要亲自去执行,全权委托第三方,替它来执行具体事物的概念。
也就是让别人间接完成本该由我来完成的事情。
委托程序的定义:
首先是一个类(Class),引用类型的数据类型
可以指向 一个或多个方法的引用。(类型兼容)
个人理解:
调用别人的方法(本身是第三方),也就是间接的使用;并且该方法可以为一个或者多个。
补充:
- C#有五类数据类型,整个数据类型分为2种:值类型(结构体,枚举)和引用数据类型(类,接口,委托)
- 事件和LAMBDA表达式的基础都是委托;而LAMBDA表达式又是LINQ的基础。
委托的声明及其由来#
委托虽然是一种类(Class),引用类型的数据类型;但是它和我们一般方法的声明格式很接近。
- <span style="color:#596172"><span style="background-color:#ffffff"><code class="language-C language-C#"> <span style="color:#5c6370"><em>//声明委托类型</em></span>
-
- public delegate <span style="color:#d19a66">void</span> <span style="color:#5c6370"><em>/*返回值类*/</em></span> MyDelegate(<span style="color:#5c6370"><em>/*参数列表*/</em></span>);
-
- <span style="color:#5c6370"><em>//这个委托类型可以指向任何一个:返值为空,参数列表为空的其他方法。</em></span>
-
- <span style="color:#5c6370"><em>/*目标方法:委托所指向的方法*/</em></span>
-
- <span style="color:#5c6370"><em>//C#允许嵌套类型,可以在类中进行委托的声明。</em></span>
- </code></span></span>
委托的声明格式是仿照C/C++[函数指针]的声明格式。
- <span style="color:#596172"><span style="background-color:#ffffff"><code class="language-C"><span style="color:#61aeee">#<span style="color:#7171bf">include</span> <span style="color:#3388aa"><stdio.h></span></span>
-
-
-
- <span style="color:#5c6370"><em>//声明一个函数指针</em></span>
-
- <span style="color:#7171bf">typedef</span> <span style="color:#61aeee">int</span>(* Calculator)(<span style="color:#d19a66">int</span> _x,<span style="color:#d19a66">int</span> _y);
-
-
-
- <span style="color:#d19a66">int</span> <span style="color:#61aeee">add</span>(<span style="color:#d19a66">int</span> _a,<span style="color:#d19a66">int</span> _b){
-
- <span style="color:#7171bf">return</span> _a + _b;
-
- }
-
-
-
- <span style="color:#d19a66">int</span> <span style="color:#61aeee">Multiply</span>(<span style="color:#d19a66">int</span> _a,<span style="color:#d19a66">int</span> _b){
-
- <span style="color:#7171bf">return</span> _a * _b;
-
- }
-
-
-
- <span style="color:#d19a66">int</span> <span style="color:#61aeee">main</span>(<span style="color:#d19a66">void</span>) {
-
- <span style="color:#d19a66">int</span> x = <span style="color:#d19a66">9</span>;
-
- <span style="color:#d19a66">int</span> y = <span style="color:#d19a66">9</span>;
-
- Calculator pointer01 = &add;
-
- Calculator pointer02 = &Multiply;
-
- <span style="color:#7171bf">printf</span>(<span style="color:#98c379">"Add Result (pointer01) : %d\n"</span>,pointer01(x,y));
-
- <span style="color:#7171bf">printf</span>(<span style="color:#98c379">"Multiply Result (pointer02) : %d\n"</span>,pointer02(x,y));
-
- <span style="color:#7171bf">return</span> <span style="color:#d19a66">0</span>;
-
- }
-
- <span style="color:#5c6370"><em>//输出:</em></span>
-
- <span style="color:#5c6370"><em>//Add Result (pointer01) : 18</em></span>
-
- <span style="color:#5c6370"><em>//Multiply Result (pointer02) : 81</em></span>
- </code></span></span>
delegate关键字也就对应了C语言种的*号(也就是指针定义符),其余都是一一对应的。
委托类型的实例#
用委托实现按空格时,物体随机移动。
- <span style="color:#596172"><span style="background-color:#ffffff"><code class="language-C language-C#">using UnityEngine;
-
-
-
- public <span style="color:#7171bf">class</span> <span style="color:#e6c07b">DelegateTest</span> : MonoBehaviour
-
- {
-
- private SpriteRenderer image;
-
-
-
- private delegate <span style="color:#d19a66">void</span> <span style="color:#61aeee">MyDelegate</span>();<span style="color:#5c6370"><em>//声明委托</em></span>
-
- private MyDelegate myDelegate;
-
-
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">Start</span>()
-
- {
-
- image = GetComponent<SpriteRenderer>();
-
- }
-
-
-
- <span style="color:#5c6370"><em>//Awake之后Start之前调用</em></span>
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">OnEnable</span>()
-
- {
-
- myDelegate = new MyDelegate(Teleport);
-
- <span style="color:#5c6370"><em>//myDelegate = Teleport;</em></span>
-
- }
-
-
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">Update</span>()
-
- {
-
- <span style="color:#7171bf">if</span> (Input.GetKeyDown(KeyCode.Space))
-
- {
-
- myDelegate.Invoke();
-
- <span style="color:#5c6370"><em>//myDelegate();</em></span>
-
- }
-
- }
-
-
-
- <span style="color:#5c6370"><em>//随机位置移动</em></span>
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">Teleport</span>()
-
- {
-
- Vector2 currentPos = transform.position;
-
- currentPos.x = UnityEngine.Random.Range(<span style="color:#d19a66">-5F</span>, <span style="color:#d19a66">5F</span>);
-
- transform.position = currentPos;
-
-
-
- }
-
- <span style="color:#5c6370"><em>//随机颜色</em></span>
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">ChangeColor</span>()
-
- {
-
- image.color = new Color(UnityEngine.Random.value,UnityEngine.Random.value,UnityEngine.Random.value);
-
- }
-
- <span style="color:#5c6370"><em>//输出时间</em></span>
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">Log</span>()
-
- {
-
- Debug.Log(<span style="color:#98c379">"Current Time is : "</span> + System.DateTime.UtcNow);
-
- }
-
- }
- </code></span></span>
多播委托#
我们都知道,计算机中的“=”是赋值,这个就是单播委托,如果想要多个方法一起调用,需要用到“+=”,也就是多播委托。
我们可以在上述的基础上,加上变色和输出,效果如下。
.
- <span style="color:#596172"><span style="background-color:#ffffff"><code class="language-C language-C#">...
-
- <span style="color:#5c6370"><em>//Awake之后Start之前调用</em></span>
-
- private <span style="color:#d19a66">void</span> <span style="color:#61aeee">OnEnable</span>()
-
- {
-
- myDelegate = new MyDelegate(Teleport);
-
- myDelegate += new MyDelegate(ChangeColor);
-
- myDelegate += new MyDelegate(Log);
-
- }
-
- ...
- </code></span></span>
委托的缺点#
委托会引用一个方法,如果这个方法是实例方法(隶属于一个对象)。这个对象会存在内存当中,从而造成内存泄漏
从而也就让事件和观察者模式的出现。
Action委托与Func委托#
C#类库中给我准备好了两个委托:Action和Func;他们最多可以有16个参数。
- Action委托
- 无返回值,可以有参数列表。
- Func委托
- 有返回值,可以有参数列表。