老鼠\猫\狗\主人晚上 老鼠出来  被猫发现  猫抓 老鼠 
这时 狗 追猫
主人被惊醒  打了狗
用C#写出程序  要求是 连动效果我觉得 应该很简单  但是 唉!!!!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        public class Program
        {
            static Actor mouse;
            static Actor cat;
            static Actor dog;
            static Actor master;
            static void Main(string[] args)
            {
                //声明变量
                mouse = new Actor("老鼠");
                cat = new Actor("猫");
                dog = new Actor("狗");
                master = new Actor("主人");            //注册事件
                mouse.BeginActEvent += new myDelegate(mouse_BeginActEvent);
                cat.BeginActEvent += new myDelegate(cat_BeginActEvent);
                dog.BeginActEvent += new myDelegate(dog_BeginActEvent);            mouse.BeginAct("觅食");            Console.ReadLine();
            }        static void dog_BeginActEvent()
            {
                Console.WriteLine("主人打狗");
            }        static void cat_BeginActEvent()
            {
                dog.BeginAct("追猫");
            }        static void mouse_BeginActEvent()
            {
                cat.BeginAct("抓老鼠");
            }
        }    public delegate void myDelegate();
        public class Actor
        {
            public event myDelegate BeginActEvent;        public Actor()
            {
                BeginActEvent = null;
            }        public Actor(string name) : this()
            {
                _Name = name;
            }        public void BeginAct(string strAction)
            {
                Console.WriteLine(_Name + strAction + "\n");
                if (BeginActEvent != null)
                {
                    BeginActEvent();
                }
            }        private string _Name;
            public string Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    _Name = value;
                }
            }
        }
    }这样可以么?
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        public class Program
        {
            static Actor mouse;
            static Actor cat;
            static Actor dog;
            static Actor master;
            static void Main(string[] args)
            {
                //声明变量
                mouse = new Actor("老鼠");
                cat = new Actor("猫");
                dog = new Actor("狗");
                master = new Actor("主人");            //注册事件
                mouse.BeginActEvent += new myDelegate(mouse_BeginActEvent);
                cat.BeginActEvent += new myDelegate(cat_BeginActEvent);
                dog.BeginActEvent += new myDelegate(dog_BeginActEvent);            mouse.BeginAct("觅食");            Console.ReadLine();
            }        static void dog_BeginActEvent(object sender)
            {
                Console.WriteLine("主人打" + (sender as Actor).Name);
            }        static void cat_BeginActEvent(object sender)
            {
                dog.BeginAct("追" + (sender as Actor).Name);
            }        static void mouse_BeginActEvent(object sender)
            {
                cat.BeginAct("抓" + (sender as Actor).Name);
            }
        }    public delegate void myDelegate(object sender);
        public class Actor
        {
            public event myDelegate BeginActEvent;        public Actor()
            {
                BeginActEvent = null;
            }        public Actor(string name) : this()
            {
                _Name = name;
            }        public void BeginAct(string strAction)
            {
                Console.WriteLine(_Name + strAction + "\n");
                if (BeginActEvent != null)
                {
                    BeginActEvent(this);
                }
            }        private string _Name;
            public string Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    _Name = value;
                }
            }
        }
    }改了一下,这样更像 OO 了
      

  3.   

    http://blog.csdn.net/johnsuna/archive/2004/12/31/235729.aspx