当前位置:网站首页>Unity C# 基础复习26——初识委托(P447)
Unity C# 基础复习26——初识委托(P447)
2022-06-29 14:39:00 【_一只小QQ】
委托Delegate
C#中的Delegate对应于C中的指针,但是又有所不同C中的指针既可以指向方法,又可以指向变量,并且可以进行类型转换,
C中的指针实际上就是内存地址变量,他是可以直接操作内存的,通过内存
地址直接访问变量,直接调用方法。
而C#中的Delegate是强类型的,也就是说在声明委托时就已经指定
了该变量只能指向具有特定参数,以及返回值的方法。
使用delegate就可以直接建立任何名称的委托类型,当进行系统编译
时,系统就会自动生成此类型。您可以使用delegate void MyDelegate()
方式建立一个委托类,并使用ILDASM.exe观察其成员。由ILDASM.exe
中可以看到,它继承了System.MulticastDelegate类,并自动生成BeginInvoke、EndInvoke、Invoke 等三个常用方法。
Invoke 方法是用于同步调用委托对象的对应方法,而BeginInvoke、EndInvoke是用于以异步方式调用对应方法的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Skill
{
public void Skill1()
{
Console.WriteLine("Skill:技能1动画");
}
public void Skill2()
{
Console.WriteLine("Skill:技能2动画");
}
public void Skill3()
{
Console.WriteLine("Skill:技能3动画");
}
public void Skill4()
{
Console.WriteLine("Skill:技能4动画");
}
public void Skill5()
{
Console.WriteLine("Skill:技能5动画");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
delegate void AutoSkillPlayer();
class Program
{
static void Main(string[] args)
{
AutoSkillPlayer akp = new AutoSkillPlayer(Skill1);
akp += Skill3;
akp += Skill4;
Skill sk = new Skill();
akp += sk.Skill4;
akp -= Skill1;
akp.Invoke();
}
public static void Skill1()
{
Console.WriteLine("技能1动画");
}
public static void Skill2()
{
Console.WriteLine("技能2动画");
}
public static void Skill3()
{
Console.WriteLine("技能3动画");
}
public static void Skill4()
{
Console.WriteLine("技能4动画");
}
public static void Skill5()
{
Console.WriteLine("技能5动画");
}
}
}

边栏推荐
- 部署搭建decentraland流程讲解
- Campus errands wechat applet errands students with live new source code
- Laravel - Composer 安装指定 Laravel 版本
- Redis installation in windows and Linux Environment
- Unity C# 基础复习29——泛型委托(P451)
- 建立自己的网站(19)
- How does a two character name become a three character name with spaces
- Analysis of constant current source circuit composed of two NPN tubes
- China soft ice cream market forecast and investment prospect research report (2022 Edition)
- Evaluation index of high concurrency software (website, server interface)
猜你喜欢

Netease strict selection offline data warehouse quality construction practice

Stm32 mbed tutorial (IV) --pwm

宜明昂科冲刺港股:年内亏损7.3亿 礼来与阳光人寿是股东

synchronized 与多线程的哪些关系

Swagger2的配置教程

云上第一课 | 建个小破站有多简单?云计算老司机带你一小时搞定

How bad can a programmer be?

EMC surge protection and decoupling design

校园转转二手市场源码

文本预处理库spaCy的基本使用(快速入门)
随机推荐
信息学奥赛一本通1003:对齐输出
phpcms打开后台首页时向官网发送升级请求觉得卡怎么办?
Asynchronous artifact completable future
How does a two character name become a three character name with spaces
网易严选离线数仓质量建设实践
Unity C# 基础复习29——泛型委托(P451)
Imitation headline news information DZ template discuz news information business version GBK template source code
Chapter 6 picture operation of canvas
Paper study -- accurate accounting of annual total runoff control rate considering the interannual variation characteristics of rainfall
宜明昂科冲刺港股:年内亏损7.3亿 礼来与阳光人寿是股东
const用法精讲
What should phpcms do when it sends an upgrade request to the official website when it opens the background home page?
synchronized 与多线程的哪些关系
在平面坐标上画斜线
You need to know about project procurement management
高並發軟件(網站,服務器端接口)的評價指標
Chapter 7 deformation operation of canvas
MCS:多元随机变量——多项式分布
中国二氧化硫脲行业研究与发展前景研究报告(2022版)
中国三氧化二砷行业研究与未来预测报告(2022版)