C#-事件(十八)
2021-06-30 01:04
标签:test 顺序 object 处理 style strong image set png 结果 C#-事件(十八) 标签:test 顺序 object 处理 style strong image set png 原文地址:https://www.cnblogs.com/tynam/p/9996859.html 1 using System;
2 namespace ConsoleApp1
3 {
4 class eventTest
5 {
6 // 声明委托
7 public delegate void EventHander(object sender, EventArgs e);
8 public string sex { get; set; } = "man";
9
10 // 定义事件,基于委托
11 public event EventHander GoToilet;
12 // 触发事件
13 public void OnEvent(EventArgs e)
14 {
15 if(this.GoToilet != null)
16 {
17 this.GoToilet(this, e);
18 }
19 }
20 public static void Toilet(object sender, EventArgs e)
21 {
22 var core = (eventTest)sender;
23 Console.WriteLine("please enter " + core.sex + " toilet");
24 }
25 static void Main(string[] args)
26 {
27 var et = new eventTest();
28 et.GoToilet += new EventHander(Toilet);
29 while (true)
30 {
31 et.OnEvent(null);
32 Console.ReadKey();
33 }
34 }
35 }
36 }