当前位置:网站首页>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
欢迎访问小程序:
边栏推荐
- canvas切积木小游戏代码
- 2.2 STM32 GPIO操作
- Explore pointers and pointer types in depth
- Overview of super-resolution reconstruction of remote sensing images
- Force buckle 1189 Maximum number of "balloons"
- [matlab] - draw a five-star red flag
- 1. New project
- 【Rust 笔记】18-宏
- A brief introduction to symbols and link libraries in C language
- Quick sort function in C language -- qsort
猜你喜欢
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
On Data Mining
Safety science to | travel, you must read a guide
three. JS page background animation liquid JS special effect
Idea push rejected solution
2.1 rtthread pin设备详解
Brush questions in summer -day3
[meisai] meisai thesis reference template
给新人工程师组员的建议
随机推荐
Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
Pytorch基础——(2)张量(tensor)的数学运算
Multi project programming minimalist use case
Cubemx 移植正点原子LCD显示例程
Map sorts according to the key value (ascending plus descending)
three.js网页背景动画液态js特效
Idea push rejected solution
JS Vanke banner rotation chart JS special effect
Crazy, thousands of netizens are exploding the company's salary
Shell pass parameters
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
Schnuka: what is visual positioning system and how to position it
Cubemx transplantation punctual atom LCD display routine
Tomb. Weekly update of Finance (February 7 - February 13)
[practice] mathematics in lottery
Containerization Foundation
User experience index system
1. New project
遥感图像超分辨重建综述
Redo file corruption repair