当前位置:网站首页>C#委托用法--控制台项目,通过委托实现事件
C#委托用法--控制台项目,通过委托实现事件
2022-07-27 20:43:00 【济南医疗小程序状元】
控制台项目,通过委托实现事件
using System;
namespace Delegate
{
internal class Program
{
#region 委托案例 在控制台输入数字,当数字不等时,触发两个事件。
// 委托 案例0.1 在控制台输入数字,当数字不等时,触发两个事件。
public class EventDemo
{
private int value;
public delegate void SpeechHandler();// 1 定义声明委托 SpeechHandler
public event SpeechHandler ChangeSpeech;// 2 根据委托定义事件 ChangeSpeech
#region AddSpe
// 0.3 事件中定义的方法. 包含在事件中,执行事件的行为动作的具体方法代码.
protected virtual void AddSpe()
{
if(ChangeSpeech !=null)
{
ChangeSpeech();
}
else
{
Console.WriteLine("语音事件触发(1)!");
}
}
#endregion
#region 事件触发(2) 方法 GetSpe
protected virtual void GetSpe()
{
if(ChangeSpeech != null)
{
ChangeSpeech();
}
else
{
Console.WriteLine("事件触发(2)!");
}
}
#endregion
#region 事件行为 方法3
protected virtual void AddSpeech()
{
if (ChangeSpeech != null)
{
ChangeSpeech();
}
else
{
Console.WriteLine("事件行为成功触发执行(3)");
}
}
#endregion
#region 设值 GZHS方式 能设置参数值的地方
public EventDemo(int n)
{
SetValue(n);
}
public void SetValue(int n)
{
if(value != n)
{
value = n;
AddSpe();
GetSpe();
//自定义事件 方法3 必须要调用事件中的方法,才能执行事件!否则事件中的方法是不会执行的.
AddSpeech();
}
}
#endregion
#region
#endregion
}
#endregion
#region 案例2
// 1. 声明一个委托(函数指针)
delegate void delegateSpeech(int a, float b);
// delegate 返回值类型 委托类型名(参数列表):
2. 创建委托对象
//public static delegateSpeech dT1;
//3. 创造符合委托格式的函数(指针指向的函数)
//public void Test(int a, float b)
//{
// a += 100;
// b *= 2.0;
// Console.WriteLine("a:{0},b:{1}", a, b);
//}
#endregion
#region
#endregion
#region
#endregion
static void Main(string[] args)
{
#region 案例1
//委托类的实例化 对象
EventDemo e = new EventDemo(0);
Console.WriteLine("请输入数字:");
while(true)
{
int i= int.Parse(Console.ReadLine());// 读取数字并且设置
e.SetValue(i);
}
#endregion
// 4. 将函数名称赋值给委托
//dT1 = new delegateSpeech(Test);
}
}
}
效果

边栏推荐
- [CVA valuation training camp] how to quickly read the annual reports of listed companies - Lesson 4
- Disable caching with meta HTML tags in all browsers
- Shuffle, partition and read of tfrecord
- [GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery
- Flink怎么使用Savepoint
- Deploy dolphin scheduler high availability cluster based on rainbow
- 8000 word explanation of OBSA principle and application practice
- 【数字识别】基于Hopfield神经网络识别0-9数字附Matlab代码
- 记录一下使用R语言中关于formatC的错误
- XML external entity (xxE) vulnerability and its repair method
猜你喜欢

Date的使用

Pro multi store version system, versatile is it!

The principle and demonstration of service path lifting without quotation marks
![[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code](/img/a7/8f0d929457d285adc8020c7f5d33ea.png)
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code

LANproxy mapping local development environment

我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律

Pyqt5 rapid development and practice 4.9 dialog controls

Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步

Introduction to the paper | language model for long text under transformer architecture

Basic SQL DDL
随机推荐
Xu Jinbo: AI protein prediction and design
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
你的列表很卡?这4个优化能让你的列表丝般顺滑
Basic SQL DML
软件测试功能测试全套常见面试题【功能测试】面试总结4-2
编辑复制粘贴判定问题(bug?),所见即所得显示符号问题反馈。
"The faster the code is written, the slower the program runs."
Basic SQL DDL
Simple and practical data visualization cases
营收、利润两位数增长,华润怡宝悄悄打造了这些过亿新品
reduce错误示范
QT with OpenGL(Shadow Mapping)(平行光篇)
With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
[signal denoising] signal denoising based on Kalman filter with matlab code
Google executives: 40% of "generation Z" are more willing to use tiktok to cannibalize Google's core products
Tita 的OKR系统与其他同类型产品,或者是共享文档等相比,有什么优势?
[C language] simulate and implement string functions (Part 1)
Basic lighting of unity
Disable caching with meta HTML tags in all browsers