C#(三十一)之自定义事件

171 阅读2分钟

「这是我参与2022首次更文挑战的第27天,活动详情查看:2022首次更文挑战

自定义事件:一定要注意下面六部曲

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();
        }
    }

 

效果图:

324324.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);
        }
   
    }
}

有好的建议,请在下方输入你的评论。

欢迎访问个人博客 guanchao.site

欢迎访问小程序:

在这里插入图片描述