事件,使用.net自带委托EventHandler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class BossDemo
    {
        public event EventHandler BossEvent; //定义.net自带的委托类型EventHandler的事件,不用自己再定义委托

        public void Onmsg(string msg)
        {
            if (BossEvent != null)
            {
                Console.WriteLine(msg);
//触发事件,需要传入参数 BossEvent(this,new EventArgs()); } } static void Main(string[] args) { BossDemo b = new BossDemo(); Worker w1 = new Worker("1"); Worker w2 = new Worker("2"); b.BossEvent += new EventHandler(w1.SendMessage); b.BossEvent += new EventHandler(w2.SendMessage); b.Onmsg("朋友你好今晚上加班"); Console.WriteLine("---------------------------"); Console.ReadKey(); } } public class Worker { private string workerId; public Worker(string wi) { this.workerId = wi; }
//该函数符合EventHandler的定义 public void SendMessage(object o,EventArgs e) { Console.WriteLine(this.workerId + "卧槽又加班"); Console.Read(); } } }

  

posted @ 2017-11-08 09:33  公众号python学习开发  阅读(285)  评论(0编辑  收藏  举报