using System;
using System.Collections.Generic;
using System.Text;

namespace DelegateEvent
{
    public delegate void CatEventHandler(object sender, EventArgs e);
    public class Cat
    {
        public event CatEventHandler CatEvent;
        public void Scream(EventArgs e)
        {
            if (CatEvent != null) //有没有订阅
            {
                Console.WriteLine("猫叫……");
                CatEvent(this, e);
            }
        }
        public void Mouse(object sender,EventArgs e)
        {
            Console.WriteLine("老鼠跑……");
        }
        public void People(object sender, EventArgs e)
        {
            Console.WriteLine("主人醒……");
        }
        static void Main()
        {
            Cat cat = new Cat();
            cat.CatEvent += new CatEventHandler(cat.Mouse);
            cat.CatEvent += new CatEventHandler(cat.People);
            cat.Scream(EventArgs.Empty);
            Console.Read();
        }
    }

}

相关文章: