当前位置:网站首页>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
欢迎访问小程序:
边栏推荐
- SAP ALV color code corresponding color (finishing)
- mysql从一个连续时间段的表中读取缺少数据
- 2、GPIO相关操作
- Canvas cut blocks game code
- UDP reliable transport protocol (quic)
- 出现Permission denied的解决办法(750权限谨慎使用)
- Multi project programming minimalist use case
- [practical exercise] face location model based on skin color
- Pytorch基础——(2)张量(tensor)的数学运算
- 深入刨析的指针(题解)
猜你喜欢
Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
Cubemx 移植正点原子LCD显示例程
canvas切积木小游戏代码
1. New project
给新人工程师组员的建议
Idea push rejected solution
Getting started with applet cloud development - getting user search content
记录一下逆向任务管理器的过程
Containerization Foundation
Pytorch load data
随机推荐
Quick sort function in C language -- qsort
指针笔试题~走近大厂
Pytorch load data
ESBuild & SWC浅谈: 新一代构建工具
Research on cooperative control of industrial robots
MySQL Server层四个日志
Deep parsing pointer and array written test questions
BUAA calculator (expression calculation - expression tree implementation)
C language circular statement
Shell pass parameters
2.2 STM32 GPIO operation
LTE CSFB test analysis
Cubemx 移植正点原子LCD显示例程
RT-Thread--Lwip之FTP(2)
Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
2.2 STM32 GPIO操作
SWC introduction
SAP ALV color code corresponding color (finishing)
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
Schnuka: visual positioning system working principle of visual positioning system