当前位置:网站首页>Custom event of C (31)
Custom event of C (31)
2022-07-06 03:47:00 【camellias_】
Custom events : Be sure to pay attention to the following six parts
1: Declare a delegate about the event
2: Claim event
3: Write the function that triggers the event
4: Create an event handler
5: Register event handlers
6: Triggering event
Defining events :
class dog
{
// 1 Declaration delegation
public delegate void dogEvent(object sender,EventArgs e);
// 2 Claim event
public event dogEvent doger;
// 3 Write the method to trigger the event
public void touch()
{
if (this.doger != null)
{
Console.WriteLine(@" Here comes the thief The dog barked .");
// The event referenced by the current object
this.doger(this,new EventArgs());
}
}
}
class host
{
// 4 Write event handlers
void hostDog(object sender, EventArgs e)
{
Console.WriteLine(" master , I caught the thief ");
}
// 5 Register event handlers
public host(dog dos)
{
// Event registration ( Definition ) Must be used +=
dos.doger += new dog.dogEvent(hostDog);
}
}
When declaring an event event It can be omitted ( It's best not to omit )
// 2 Claim event
public dogEvent doger;
Event succession
event event It's all inherited EventArgs, By inheritance EventArgs You can add parameters to custom events .
public class AlarmEventArgs : EventArgs
{
// Define the number of thieves
public int numberOfThieves;
// Assign a value to the number of thieves through the constructor
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(" Time passes again ");
while (now < minignt)
{
Console.WriteLine(" current time "+now);
// Sleep for a second
System.Threading.Thread.Sleep(1000);
// Add one second to the current time
now = now.AddSeconds(1);
}
// Assign a value to the number of thieves
AlarmEventArgs e = new AlarmEventArgs(3);
// Call the trigger event method
dosa.touch(e);
Console.ReadKey();
}
}
design sketch :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-pieLUA3K-1656122855547)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e4b07d6147c74508a97c3e6a0f8679d9~tplv-k3u1fbpfcp-zoom-1.image “1556633488315290.png”)]
The test uses all the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @event
{
public class AlarmEventArgs : EventArgs
{
// Define the number of thieves
public int numberOfThieves;
// Assign a value to the number of thieves through the constructor
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(" Time passes again ");
while (now < minignt)
{
Console.WriteLine(" current time "+now);
// Sleep for a second
System.Threading.Thread.Sleep(1000);
// Add one second to the current time
now = now.AddSeconds(1);
}
// Assign a value to the number of thieves
AlarmEventArgs e = new AlarmEventArgs(3);
// Call the trigger event method
dosa.touch(e);
Console.ReadKey();
}
}
class dog
{
// 1 Declaration delegation
public delegate void dogEvent(object sender,AlarmEventArgs e);
// 2 Claim event
public event dogEvent doger;
// 3 Write the method to trigger the event
public void touch(AlarmEventArgs e)
{
if (this.doger != null)
{
Console.WriteLine(@" Here comes the thief The dog barked .");
// The event referenced by the current object
//this.doger(this,new EventArgs());
this.doger(this, e);
}
}
}
class host
{
// 4 Write event handlers
void hostDog(object sender, AlarmEventArgs e)
{
if(e.numberOfThieves <= 1)
{
Console.WriteLine(" master , I caught the thief ");
}
else
{
Console.WriteLine(" master , Too many people , Call the police ");
}
}
// 5 Register event handlers
public host(dog dos)
{
// Event registration ( Definition ) Must be used +=
dos.doger += new dog.dogEvent(hostDog);
}
}
}
There are good suggestions , Please enter your comments below .
Welcome to personal blog
https://guanchao.site
Welcome to the applet :
边栏推荐
- No qualifying bean of type ‘......‘ available
- How to standardize the deployment of automated testing?
- Cross origin cross domain request
- Force buckle 1189 Maximum number of "balloons"
- An article will give you a comprehensive understanding of the internal and external components of "computer"
- Factors affecting user perception
- SWC introduction
- MySQL reads missing data from a table in a continuous period of time
- Pytorch load data
- BUAA calculator (expression calculation - expression tree implementation)
猜你喜欢
User experience index system
How to standardize the deployment of automated testing?
Mysqldump data backup
C language judgment, ternary operation and switch statement usage
2.13 weekly report
Pytorch基础——(1)张量(tensor)的初始化
2.2 STM32 GPIO操作
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
Blue Bridge Cup - Castle formula
JS Vanke banner rotation chart JS special effect
随机推荐
RT-Thread--Lwip之FTP(2)
[practice] mathematics in lottery
Oracle ORA error message
[rust notes] 18 macro
Quartz misfire missed and compensated execution
Shell pass parameters
MySQL about self growth
Deno介绍
Flask learning and project practice 9: WTF form verification
[Massey] Massey font format and typesetting requirements
2.1 rtthread pin设备详解
自动化测试怎么规范部署?
After five years of testing in byte, I was ruthlessly dismissed in July, hoping to wake up my brother who was paddling
LTE CSFB test analysis
JS Vanke banner rotation chart JS special effect
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
Introduction to DeNO
Pytorch基础——(1)张量(tensor)的初始化
Pointer for in-depth analysis (problem solution)
How to standardize the deployment of automated testing?