当前位置:网站首页>C#(三十一)之自定义事件
C#(三十一)之自定义事件
2022-07-06 03:39:00 【camellias_】
自定义事件:一定要注意下面六部曲
1:声明关于事件的委托
2:声明事件
3:编写触发事件的函数
4:创建事件处理程序
5:注册事件处理程序
6:触发事件
定义事件:
class dog
{
// 1 声明委托
public delegate void dogEvent(object sender,EventArgs e);
// 2 声明事件
public event dogEvent doger;
// 3 编写触发事件的方法
public void touch()
{
if (this.doger != null)
{
Console.WriteLine(@"来小偷了 狗叫了。");
// 当前对象所引用的事件
this.doger(this,new EventArgs());
}
}
}
class host
{
// 4 编写事件处理程序
void hostDog(object sender, EventArgs e)
{
Console.WriteLine("主人,我抓住了小偷");
}
// 5 注册事件处理程序
public host(dog dos)
{
// 事件注册(定义)必须用到 +=
dos.doger += new dog.dogEvent(hostDog);
}
}
声明事件时event可以省略(最好不要省略)
// 2 声明事件
public dogEvent doger;
事件继承
事件event都是继承自EventArgs,通过继承EventArgs可以给自定义事件加参数。
public class AlarmEventArgs : EventArgs
{
// 定义小偷数量
public int numberOfThieves;
// 通过构造函数给小偷数量赋值
public AlarmEventArgs(int numberValue)
{
numberOfThieves = numberValue;
}
}
class Program
{
static void Main(string[] args)
{
dog dosa = new dog();
host ho = new host(dosa);
DateTime now = new DateTime(2019,12,31,23,59,50);
DateTime minignt = new DateTime(2020,1,1,0,0,0);
Console.WriteLine("时间再流逝");
while (now < minignt)
{
Console.WriteLine("当前时间"+now);
// 睡一秒
System.Threading.Thread.Sleep(1000);
// 当前时间加一秒
now = now.AddSeconds(1);
}
// 给小偷数量赋值
AlarmEventArgs e = new AlarmEventArgs(3);
// 调用触发事件方法
dosa.touch(e);
Console.ReadKey();
}
}
效果图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pieLUA3K-1656122855547)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e4b07d6147c74508a97c3e6a0f8679d9~tplv-k3u1fbpfcp-zoom-1.image “1556633488315290.png”)]
测试使用全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @event
{
public class AlarmEventArgs : EventArgs
{
// 定义小偷数量
public int numberOfThieves;
// 通过构造函数给小偷数量赋值
public AlarmEventArgs(int numberValue)
{
numberOfThieves = numberValue;
}
}
class Program
{
static void Main(string[] args)
{
dog dosa = new dog();
host ho = new host(dosa);
DateTime now = new DateTime(2019,12,31,23,59,50);
DateTime minignt = new DateTime(2020,1,1,0,0,0);
Console.WriteLine("时间再流逝");
while (now < minignt)
{
Console.WriteLine("当前时间"+now);
// 睡一秒
System.Threading.Thread.Sleep(1000);
// 当前时间加一秒
now = now.AddSeconds(1);
}
// 给小偷数量赋值
AlarmEventArgs e = new AlarmEventArgs(3);
// 调用触发事件方法
dosa.touch(e);
Console.ReadKey();
}
}
class dog
{
// 1 声明委托
public delegate void dogEvent(object sender,AlarmEventArgs e);
// 2 声明事件
public event dogEvent doger;
// 3 编写触发事件的方法
public void touch(AlarmEventArgs e)
{
if (this.doger != null)
{
Console.WriteLine(@"来小偷了 狗叫了。");
// 当前对象所引用的事件
//this.doger(this,new EventArgs());
this.doger(this, e);
}
}
}
class host
{
// 4 编写事件处理程序
void hostDog(object sender, AlarmEventArgs e)
{
if(e.numberOfThieves <= 1)
{
Console.WriteLine("主人,我抓住了小偷");
}
else
{
Console.WriteLine("主人,人太多,赶紧报警");
}
}
// 5 注册事件处理程序
public host(dog dos)
{
// 事件注册(定义)必须用到 +=
dos.doger += new dog.dogEvent(hostDog);
}
}
}
有好的建议,请在下方输入你的评论。
欢迎访问个人博客
https://guanchao.site
欢迎访问小程序:

边栏推荐
- 1.16 - 校验码
- Canvas cut blocks game code
- Safety science to | travel, you must read a guide
- Blue style mall website footer code
- Pointer for in-depth analysis (problem solution)
- Image super-resolution using deep convolutional networks(SRCNN)解读与实现
- SAP ALV颜色代码对应颜色(整理)
- Yyds dry inventory what is test driven development
- [Qt5] QT QWidget immediately appears and disappears
- BUAA calculator (expression calculation - expression tree implementation)
猜你喜欢

User experience index system

1.16 - check code

2.2 STM32 GPIO操作

Python implementation of maddpg - (1) openai maddpg environment configuration

Schnuka: 3D vision detection application industry machine vision 3D detection

Tidb ecological tools (backup, migration, import / export) collation

Explore pointers and pointer types in depth
![[Massey] Massey font format and typesetting requirements](/img/27/6b641551d6d8699683972f40f3b8e5.jpg)
[Massey] Massey font format and typesetting requirements

SWC introduction

2、GPIO相关操作
随机推荐
Quartz misfire missed and compensated execution
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
ESBuild & SWC浅谈: 新一代构建工具
MySQL Server层四个日志
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
UDP reliable transport protocol (quic)
RT thread -- FTP of LwIP (2)
BUAA magpie nesting
SWC introduction
SAP ALV颜色代码对应颜色(整理)
Shell pass parameters
Deno介绍
[practical exercise] face location model based on skin color
数据分析——seaborn可视化(笔记自用)
[Li Kou] the second set of the 280 Li Kou weekly match
3分钟带你了解微信小程序开发
IPv6 comprehensive experiment
Cubemx 移植正点原子LCD显示例程
2.1 rtthread pin device details
JS Vanke banner rotation chart JS special effect