当前位置:网站首页>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动画");
}
}
}

边栏推荐
- Configuration tutorial for swagger2
- How does a two character name become a three character name with spaces
- MCS:离散随机变量——Binomial分布
- 自动注入@Resource和@Autowired注解的区别:
- 校园转转二手市场源码
- 织梦dedecms资源素材教程下载网站模板源码(带手机移动端)附安装教程
- 《canvas》之第13章 事件操作
- Class template case - array class encapsulation
- Chapter 11 of canvas canvas status
- Whitelabel error page access
猜你喜欢

阿尔兹海默病智能诊断

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

Source code of campus secondary market

论文学习——考虑场次降雨年际变化特征的年径流总量控制率准确核算

Zhimeng dedecms resource material tutorial download website template source code (with mobile terminal) with installation tutorial

卫龙更新招股书:年营收48亿 创始人刘卫平家族色彩浓厚
![[Verilog quick start of Niuke online question series] ~ shift operation and multiplication](/img/ea/457abb2ad0d39ffe4b235576c49a8f.png)
[Verilog quick start of Niuke online question series] ~ shift operation and multiplication

Uniapp problem list and experience

Opengauss community establishes sig knowledgegraph

Huali biology rushes to the scientific innovation board: the annual revenue is RMB 226million and it is planned to raise RMB 800million
随机推荐
Huashu high tech rushes to the scientific innovation board: the actual controller xuxiaoshu and his son, who plan to raise 660million yuan, are both American nationals
EMC surge protection and decoupling design
Chapter 14 of canvas physical animation
信息学奥赛一本通1003:对齐输出
期货开户可以线下开户吗?在网上开户安全吗?
精品商城拼团秒杀优惠折扣全功能完美双端自适应对接个人免签网站源码
Huali biology rushes to the scientific innovation board: the annual revenue is RMB 226million and it is planned to raise RMB 800million
Chapter 7 deformation operation of canvas
synchronized 与多线程的哪些关系
[practical chapter of correlation analysis] why can't Bi software do correlation analysis
MCS:离散随机变量——Hyper Geometric分布
Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
建立自己的网站(19)
Chapter 6 picture operation of canvas
How does a two character name become a three character name with spaces
. Net program configuration file operation (INI, CFG, config)
自动注入@Resource和@Autowired注解的区别:
Unity C# 基础复习29——泛型委托(P451)
I want to search the hundreds of nodes in the data warehouse. Can I check a table used in the SQL
中国三氧化二砷行业研究与未来预测报告(2022版)