当前位置:网站首页>C#中委托的应用
C#中委托的应用
2022-06-09 20:25:00 【charlsdm】
C#中委托的应用案例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp18
{
class Monster
{
public Action<Monster> deadDoSomething;
public int money = 10;
public void Dead()
{
if(deadDoSomething!=null)
{
deadDoSomething(this);
}
deadDoSomething = null;
}
}
class Player
{
private int myMoney = 0;
public void MonsterDeadDoSomething(Monster m)
{
myMoney += m.money;
Console.WriteLine("现在有{0}钱", myMoney);
}
}
class Panel
{
private int nowShowMoney = 0;
public void MonsterDeadDoSomething(Monster m)
{
nowShowMoney += m.money;
Console.WriteLine("现在有{0}元钱", nowShowMoney);
}
}
class CJ
{
private int nowKillMonsterNum = 0;
public void MonsterDeadDo(Monster m)
{
nowKillMonsterNum += 1;
Console.WriteLine("当前击杀了{0}怪物", nowKillMonsterNum);
}
}
class Program
{
static void Main(string[] args)
{
Monster monster = new Monster();
Player p = new Player();
Panel panel = new Panel();
CJ cj = new CJ();
monster.deadDoSomething += p.MonsterDeadDoSomething;
monster.deadDoSomething += panel.MonsterDeadDoSomething;
monster.deadDoSomething += cj.MonsterDeadDo;
monster.Dead();
Monster monster2 = new Monster();
monster2.deadDoSomething += p.MonsterDeadDoSomething;
monster2.deadDoSomething += panel.MonsterDeadDoSomething;
monster2.deadDoSomething += cj.MonsterDeadDo;
monster2.Dead();
}
}
}
边栏推荐
- neo4j图数据库导出csv文件
- Download the latest global epidemic history data to the two days before the current day
- [opencvsharpdnn] implementation example of yolov3 and Caffe in opencvsharp
- Jerry's chart confirms that the project has not opened config_ NO_ SDRAM_ Enable macro, SDRAM used in the project [chapter]
- shell script安装prometheus和node_exporter
- ASP Err. Detailed description of number error
- 【RK2206】4. Mqtt example
- Pyqt QWidget prohibits operations that are not current windows
- 深夜小酌,50道经典SQL题,真香~
- 2022年全国最新消防设施操作员(高级消防设施操作员)考试题库及答案
猜你喜欢

Unity将Project升级至URP

More than observation | Alibaba cloud observable Technology Summit officially launched

Unity upgrade project to URP

Download the latest global epidemic history data to the two days before the current day

目标分割之--Unet对多类别数据集的语义分割

MySQL data type

2022年全国最新消防设施操作员(高级消防设施操作员)考试题库及答案

ConvNets Principles

SSL(Secure socket Layer)数字证书

HMI 联机下载失败的问题及解决方案
随机推荐
Unity upgrade project to URP
2022 gdcpc Guangdong undergraduate program design competition
ASP Err. Detailed description of number error
杰理之蓝牙配网【篇】
Common regular expressions
Query SQL
Jerry's IO_ Key how to use double keys? [chapter]
Lammps中常用的势函数和晶体库资源收集
C#中委托与事件之间的一些应用
牛客网:关于第K个数的一些排序问题
深夜小酌,50道经典SQL题,真香~
Leetcode 1984. 学生分数的最小差值(可以,已解决)
Tips for writing code
卡尔曼滤波(KF)无迹卡尔曼滤波(UKF)
Jerry's image transmission, recording card or camera displayed by UVC - description of high frame rate [article]
Safety net interview (Miscellaneous)
2022年全国最新消防设施操作员(高级消防设施操作员)考试题库及答案
如何查看 Tekton 的流水线指标
高通:将坚持多元化的代工战略,关注英特尔代工商务合作条件
Jerry's application of camera [chapter]