委托与事件

  public class Cat
    {
        public delegate void MaojiaoHandler();
        public event MaojiaoHandler Maojiao;

        public void RaiseMaojiaoEvent()
        {
            if (Maojiao != null)  //不等于空说明有客户订阅了该事件
            {
                Delegate[] dels = Maojiao.GetInvocationList();
                foreach (MaojiaoHandler del in dels)
                {
                    del();
                }


                Maojiao();
            }
            else
            {
                Console.WriteLine("没有发现事件订阅者...");
            }
        }

        public void MaoHuoDong()
        {
            Console.WriteLine("猫在睡觉....");
            Console.WriteLine("猫在醒了.发现了老鼠,猫叫了.喵...");
            RaiseMaojiaoEvent();
        }

    }

    public class Mouse
    {
        public void Run()
        {
            Console.WriteLine("猫来了,快跑...");
        }
    }

 

posted @ 2012-12-23 22:39  玻璃鱼儿  阅读(131)  评论(0编辑  收藏  举报