C#委托事件的理解

public delegate void CatListeningHandler();

class Cat

   

        public event CatListeningHandler CatCry

 

        public void Cry()

        {

            Console.WriteLine("Meow~~~");

            OnCry();

        }

 

        protected virtual void OnCry()

        {

            if (CatCry != null)

            {

                CatCry();

            }

        }

    }

 

    class Mouse

    {

        public void Run()

        {

            Console.WriteLine("Mouse run away……");

        }

}

    class Program

    {

        static void Main(string[] args)

        {

 

            Cat obj_cat = new Cat();

            Mouse obj_mouse = new Mouse();

            obj_cat.CatCry += new CatListeningHandler(obj_mouse.Run);

            obj_cat.Cry();

            Console.ReadLine();

        }

    }

 

posted @ 2009-05-04 11:44  亦心  阅读(415)  评论(0编辑  收藏  举报