猫大叫,鼠速逃,人醒了

  一道.Net程序员面试题的讨论:猫大叫,鼠速逃,人醒了。

考虑:
(1)联动性
(2)可扩展性

编程实现它。
试了一下.

  1 using System;
  2 
  3 namespace demo_2
  4 {
  5     /// <summary>
  6     /// Class2 的摘要说明。
  7     /// </summary>
  8     public delegate void delegate_handler(object sender,EventArgs e);
  9     public class Class2
 10     {
 11         
 12         /// <summary>
 13         /// 应用程序的主入口点。
 14         /// </summary>
 15         [STAThread]
 16         static void Main(string[] args)
 17         {
 18             Cat  objCat = new Cat();
 19             Mouse  objMouse=new Mouse();
 20             Man  objMan=new Man();
 21             objCat.add_EventHandler(new delegate_handler(objMouse.run));
 22             objMouse.add_EventHandler(new delegate_handler(objMan.wakeup));
 23             objCat.CatCry();
 24             
 25         }
 26     }
 27     public  class  Cat
 28     {
 29         public Cat()
 30         {
 31             Console.WriteLine("产生猫");
 32         }
 33         public event delegate_handler Cry; 
 34         protected virtual void OnCry(EventArgs e) 
 35         { 
 36             if(Cry != null
 37             { 
 38                 Cry(this,e); 
 39             } 
 40         }
 41         public void add_EventHandler(delegate_handler dh)
 42         {
 43             if(dh==null)
 44             {
 45                 Cry=dh;
 46             }
 47             else
 48             {
 49                 Cry +=dh; 
 50             }
 51 
 52         }
 53         public  void   CatCry()
 54         {
 55             Console.WriteLine("猫叫了"); 
 56             System.Threading.Thread.Sleep(500); 
 57             OnCry(new System.EventArgs());
 58         }
 59     } 
 60     public  class  Mouse
 61     {
 62         public Mouse()
 63         {
 64             Console.WriteLine("产生老鼠");
 65         }
 66         public event delegate_handler MouseRun;
 67         protected virtual void OnRun(EventArgs e) 
 68         { 
 69             if(MouseRun != null
 70             { 
 71                 MouseRun(this,e); 
 72             } 
 73         }
 74         public void add_EventHandler(delegate_handler dh)
 75         {
 76             if(dh==null)
 77             {
 78                 MouseRun=dh;
 79             }
 80             else
 81             {
 82                 MouseRun +=dh; 
 83             }
 84 
 85         }
 86         public void run(object sender,EventArgs e) 
 87         { 
 88             Console.WriteLine("老鼠逃了"); 
 89             System.Threading.Thread.Sleep(500); 
 90             OnRun(new System.EventArgs());
 91         } 
 92     }
 93     public  class Man
 94     {
 95         public Man()
 96         {
 97             Console.WriteLine("产生人");
 98         }
 99         public void wakeup(object sender,EventArgs e) 
100         { 
101             Console.WriteLine("人醒了"); 
102         } 
103     }
104 }

 

posted @ 2007-02-27 17:23  人月  阅读(511)  评论(6编辑  收藏  举报