猫叫,老鼠逃跑,主人惊醒

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

namespace test
{
    //猫叫,老鼠逃跑,主人惊醒
    class Program
    {
        static void Main(string[] args)
        {
            Cat c = new Cat("加菲猫");
            Mouse m = new Mouse("米老鼠");
            Person p = new Person("汤姆");

           c.MyEvent += new NumberChangedEventHandler(c.OnDoSomeThing);
           c.MyEvent += new NumberChangedEventHandler(m.OnDoSomeThing);
            c.MyEvent += new NumberChangedEventHandler(p.OnDoSomeThing);

            c.Cry();//猫叫
            Console.Read();
         
        }

       

    }
    public delegate void NumberChangedEventHandler();
    public class Cat 
    {
        public event NumberChangedEventHandler MyEvent;
        private string name;
        public Cat(string name)
        {
            this.name = name;
        }

        public void Cry()
        {
            if (MyEvent != null)
            {
                Delegate[] delegates = MyEvent.GetInvocationList();
                foreach (Delegate del in delegates)
                {
                    object o = del.DynamicInvoke();
                }

            }
        }

        public void OnDoSomeThing()
        {
            Console.WriteLine("猫" + name + "叫");
        }
    }

 
    public class Mouse 
    {
        private string name;
        public Mouse(string name)
        {
            this.name = name;
        }

        public   void OnDoSomeThing()
        {
            Console.WriteLine("老鼠" + name + "跑");
        }
       
    }

  
    public class Person 
    { 
        private string name;
        public Person(string name)
        {
            this.name = name;
        }

        public   void OnDoSomeThing()
        {
            Console.WriteLine("主人" + name + "惊醒");
        }
    }
}

 

 

 

错误请指正

posted @ 2011-05-10 14:36 小小部落 阅读(...) 评论(...) 编辑 收藏