当前位置:网站首页>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
欢迎访问小程序:
边栏推荐
- The solution of permission denied (750 permissions should be used with caution)
- ArabellaCPC 2019(补题)
- Getting started with applet cloud development - getting user search content
- BUAA calculator (expression calculation - expression tree implementation)
- MPLS experiment
- UDP reliable transport protocol (quic)
- Tomb. Weekly update of Finance (February 7 - February 13)
- 【RISC-V】外部中断
- Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
- Idea push rejected solution
猜你喜欢
Brush questions in summer -day3
[Massey] Massey font format and typesetting requirements
IPv6 comprehensive experiment
Force buckle 1189 Maximum number of "balloons"
2.1 rtthread pin device details
3.1 rtthread 串口设备(V1)详解
遥感图像超分辨率论文推荐
BUAA magpie nesting
[Li Kou] the second set of the 280 Li Kou weekly match
深入刨析的指针(题解)
随机推荐
SWC introduction
Quick sort function in C language -- qsort
Leetcode problem solving -- 173 Binary search tree iterator
Deno介绍
[American competition] mathematical terms
Deep parsing pointer and array written test questions
An article will give you a comprehensive understanding of the internal and external components of "computer"
1、工程新建
The solution of permission denied (750 permissions should be used with caution)
ESBuild & SWC浅谈: 新一代构建工具
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
蓝色样式商城网站页脚代码
Tidb ecological tools (backup, migration, import / export) collation
Overview of super-resolution reconstruction of remote sensing images
BUAA喜鹊筑巢
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
3.1 rtthread 串口设备(V1)详解
【RISC-V】外部中断
Precautions for single chip microcomputer anti reverse connection circuit
Leetcode problem solving -- 99 Restore binary search tree