C# 事件

事件:

    //定义委托
    public delegate void PubComputerEventHandler(object sender, MyEventArgs e);
    public delegate void PubLifeEventHandler(object sender, MyEventArgs e);

    //封装EventArgs类
    public class MyEventArgs : EventArgs
    {
        private readonly string m_magazineName; //杂志名称
        private readonly DateTime m_pubDate;    //出刊日期
        public MyEventArgs(string magazineName, DateTime pubDate)
        {
            m_magazineName = magazineName;
            m_pubDate = pubDate;
        }
        public string magazineName
        {
            get { return m_magazineName; }
        }
        public DateTime pubDate
        {
            get { return m_pubDate; }
        }
    }

    //出版社类
    public class Publisher
    {
        //出版社名
        protected string _pubName;
        public Publisher(string pubName)
        {
            _pubName = pubName;
        }
        public Publisher() { }
        //电脑杂志事件
        public event PubComputerEventHandler PubComputer;     //事件,只能在出版社类内部被调用,外面只能通过+=或-=赋值,不能直接调用它。

        protected virtual void OnPubComputer(Publisher pub, MyEventArgs e)  //虚方法,派生类中可调用。
        {
            PubComputerEventHandler handler = pub.PubComputer;  //定义临时的委托
            if (handler != null)
            {
                Console.WriteLine(string.Format("{0}:发行了电脑杂志【{1}】 {2}", pub._pubName, e.magazineName, e.pubDate.ToString()));
                System.Threading.Thread.Sleep(1000);
                handler(this, e);
            }
        }

        //生活杂志事件
        public event PubLifeEventHandler PubLife;

        protected virtual void OnPubLife(Publisher pub, MyEventArgs e)
        {
            if (pub.PubLife != null)
            {
                Console.WriteLine(string.Format("{0}:发行了生活杂志【{1}】 {2}", pub._pubName, e.magazineName, e.pubDate.ToString()));
                System.Threading.Thread.Sleep(1000);
                pub.PubLife(this, e);
            }
        }

    }

    //管理员类
    public class Manager : Publisher
    {
        //发行电脑杂志
        public void IssuePubComputer(Publisher pub, string magezineName, DateTime pubDate)
        {
            OnPubComputer(pub, new MyEventArgs(magezineName, pubDate));
        }
        //发行生活杂志
        public void IssuePubLife(Publisher pub, string magezineName, DateTime pubDate)
        {
            OnPubLife(pub, new MyEventArgs(magezineName, pubDate));
        }
    }

    //订阅者类
    public class Subscriber
    {
        private string name;
        public Subscriber(string name)
        {
            this.name = name;
        }
        public void Receive(object sender, MyEventArgs e)
        {
            Console.WriteLine("{0} {1} 已经收到“{2}”", DateTime.Now.ToString(), name, e.magazineName);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            //实例出版社对象: 清华出版社qh_Pub
            Publisher qh_Pub = new Publisher("清华出版社");

            //订阅者用户对象
            Subscriber zs = new Subscriber("张三");
            Subscriber ls = new Subscriber("李四");

            //开始订阅
            qh_Pub.PubComputer += new PubComputerEventHandler(zs.Receive); //订阅电脑杂志
            qh_Pub.PubComputer += new PubComputerEventHandler(ls.Receive); //订阅电脑杂志
            qh_Pub.PubLife += new PubLifeEventHandler(ls.Receive);         //订阅生活杂志

            //管理员定期发刊杂志
            Manager manager = new Manager();
            manager.IssuePubComputer(qh_Pub, "苦逼程序猿㈠", DateTime.Now); //清华出版社发刊
            SleepTime(3);
            manager.IssuePubLife(qh_Pub, "365百姓话题v1", DateTime.Now); //清华出版社发刊

            //=============================================================================
            //一个月后,李四取消了电脑杂志,王五订阅了生活杂志
            Console.WriteLine();
            Console.WriteLine("一个月后。。。");
            SleepTime(5);
            Subscriber ww = new Subscriber("王五");
            qh_Pub.PubComputer -= new PubComputerEventHandler(ls.Receive); //取消电脑杂志
            qh_Pub.PubLife += new PubLifeEventHandler(ww.Receive);         //订阅生活杂志

            //管理员定期发刊杂志
            manager.IssuePubComputer(qh_Pub, "苦逼程序猿㈡", DateTime.Now); //清华出版社发刊
            SleepTime(3);
            manager.IssuePubLife(qh_Pub, "365百姓话题v2", DateTime.Now); //清华出版社发刊

            Console.ReadKey();
        }

        private static void SleepTime(int n)
        {
            DateTime dt = DateTime.Now;
            do
            {
                Console.Write(".");
                System.Threading.Thread.Sleep(1000);
            } while ((DateTime.Now - dt).Seconds < n);
            Console.WriteLine();
        }
    }

posted @ 2014-11-01 23:13  hg一如既往  阅读(325)  评论(0编辑  收藏  举报