要求用c#编写,要有可扩展性,可实用性,
题目:
猫大叫,老鼠到处逃跑,主人醒

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3997/3997199.xml?temp=.6227838看这里
      

  2.   

    这个很好,感谢saucer.
     
    回复人: saucer(思归) ( ) 信誉:338  2005-5-13 1:14:20  得分: 50  
     
     
    this is the observer pattern, a typical implementation in .NET is to use event, for exampleusing System;class Cat
    {
      public event EventHandler Mew;  protected virtual void OnMew (EventArgs e)
      {
    if (Mew != null)
      Mew(this,e);
      }  public void DoMew()
      {
    Console.WriteLine("Cat mewed, raising an event...");
    OnMew(EventArgs.Empty);
      }
    }class Mouse
    {
      public void Run()
      {
    Console.WriteLine("Mouse ran...");
      }  public void OnCatMew(Object sender, EventArgs e)
      {
    Console.WriteLine("Mouse heard the cat mewed...");
    Run();
      }
    }class Owner
    {
      public void Awake()
      {
    Console.WriteLine("Owner awoke..");
      }  public void OnNoise(Object sender, EventArgs e)
      {
    if (sender is Cat)
    Console.WriteLine("Owner heard the cat mewed....");
    else
    Console.WriteLine("Owner heard some other noise...");
    Awake();
      }
    }class TestMew
    {
      public static void Main()
      {
    Cat c = new Cat();
    Mouse m = new Mouse();
    Owner o = new Owner();c.Mew += new EventHandler(m.OnCatMew);
    c.Mew += new EventHandler(o.OnNoise);c.DoMew();
      }
    }  
     
      

  3.   

    I 'm the author of this test paper
    see my blog:
    http://elwin.blogchina.com/913935.html