Event-2

使用自带的EventHandler

using System;
//user the EventHandler to implment the event
namespace Event2
{ 
    
    class GroupOwner
    {
        public string str;
        public event EventHandler broadCastEvent;

        public void broadCastMsg(string msg)
        {
            if (broadCastEvent != null)
            {
                str = msg;
                broadCastEvent(this, new EventArgs());
            }
        }
    }
    class Member
    {
        private string _name;
        public Member(string name)
        {
            _name = name;   
        }

        public void ReceiveMsg(Object s,EventArgs e)
        {
            GroupOwner owner = (GroupOwner)s;
            String msg = owner.str;
            Console.WriteLine($"【{_name}】has received the message-------->{msg}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            GroupOwner owner = new GroupOwner();
            Member m1 = new Member("Java");
            Member m2 = new Member("C#");
            Member m3 = new Member("C/C++");
            Member m4 = new Member("Js");
            owner.broadCastEvent += new EventHandler(m1.ReceiveMsg);
            owner.broadCastEvent += new EventHandler(m2.ReceiveMsg);
            owner.broadCastEvent += new EventHandler(m3.ReceiveMsg);
            owner.broadCastEvent += new EventHandler(m4.ReceiveMsg);
            owner.broadCastEvent -= new EventHandler(m1.ReceiveMsg);


            owner.broadCastMsg("Hello,EveryBody,all of you have a day off tomorrow");
        }
    }
}

Event-2_第1张图片

扩展EventArgs

using System;
namespace Event2
{

    class GroupOwner
    {
        public delegate void MyHandler(Object o, MyEventArgs args);
        public event MyHandler broadCastEvent;

        public void broadCastMsg(string msg)
        {
            if (broadCastEvent != null)
            {
                int no = new Random().Next();
                string info = msg;
                string sender = "群主";
                DateTime t = DateTime.Now;
                Msg message = new Msg(no,info,sender,t);               
                broadCastEvent(this, new MyEventArgs(message));
            }
        }
    }
    class Member
    {
        private string _name;
        public Member(string name)
        {
            _name = name;
        }

        public void ReceiveMsg(Object s, MyEventArgs e)
        {
            int no = e.message.getNo();
            string info = e.message.getInfo();
            string sender = e.message.getSender();
            DateTime t = e.message.getTime();

            Console.WriteLine($"【{sender}: {info}\t{t}");
            Console.WriteLine($"({_name} received successfully!)\r\n");

        }
    }

    class MyEventArgs : EventArgs
    {
        public Msg message;

        public MyEventArgs(Msg msg)
        {
            message = msg;
        }


    }

    class Msg
    {
        private int no;
        private string msgInfo;
        private string sender;
        private DateTime time;

        public Msg(int n,string content,string s,DateTime t)
        {
            no = n;
            msgInfo = content;
            time = t;
            sender = s;

        }
        public int getNo() => no;
        public string getInfo() => msgInfo;
        public string getSender() => sender;
        public DateTime getTime() => time;
    }

    class Program
    {
        static void Main(string[] args)
        {
            GroupOwner owner = new GroupOwner();
            Member m1 = new Member("张飞");
            Member m2 = new Member("关羽");
            Member m3 = new Member("刘备");
            Member m4 = new Member("赵子龙");
            owner.broadCastEvent += new GroupOwner.MyHandler(m1.ReceiveMsg);
            owner.broadCastEvent += new GroupOwner.MyHandler(m2.ReceiveMsg);
            owner.broadCastEvent += new GroupOwner.MyHandler(m3.ReceiveMsg);
            owner.broadCastEvent += new GroupOwner.MyHandler(m4.ReceiveMsg);
            owner.broadCastEvent -= new GroupOwner.MyHandler(m1.ReceiveMsg);


            owner.broadCastMsg("Hello,EveryBody,all of you have a day off tomorrow");

            Console.ReadKey();
        }
    }
}

Event-2_第2张图片

你可能感兴趣的:(C#)