class CatMouseMaster
    {
        public delegate void CatShouter();
        public event CatShouter EventShouter;        public void CatCry()
        {
            Console.WriteLine("Cat Cry!");
            this.EventShouter += new CatShouter(mouseRun);
        }
        public void mouseRun()
        {
            if (EventShouter !=null)
            {
                Console.WriteLine("mouse Run");
                this.EventShouter +=new CatShouter(masterWakeup);
            }
            else
            {
                Console.WriteLine("mouse not Run");
            }
     
        
        }
        public void masterWakeup()
        {
            if (EventShouter!=null)
            {
                Console.WriteLine("master Wake up");
            }
            else
            {
                Console.WriteLine("master not Wake up");
            }
        }
有问题吗?怎么结果只有Cat Cry! 没有mouse Run和master Wake up?哪有问题

解决方案 »

  1.   

    1using   System;   
    2using   System.Collections;   


    5namespace   ConsoleApplication1   
    6{   
    7   public   delegate   void   SubEventHandler();   
    8   public   abstract   class   Subject   
    9   {   
    10   public   event   SubEventHandler   SubEvent;   
    11   protected   void   FireAway()   
    12   {   
    13   if   (this.SubEvent   !=   null)   
    14   this.SubEvent();   
    15   }   
    16   }   
    17   public   class   Cat   :   Subject   
    18   {   
    19   public   void   Cry()   
    20   {   
    21   Console.WriteLine( "cat   cryed. ");   
    22   this.FireAway();   
    23   }   
    24   }   
    25 
    26   public   abstract   class   Observer   
    27   {   
    28   public   Observer(Subject   sub)   
    29   {   
    30   sub.SubEvent   +=   new   SubEventHandler(Response);   
    31   }   
    32   public   abstract   void   Response();   
    33   }   
    34   public   class   Mouse   :   Observer   
    35   {   
    36   private   string   name;   
    37   public   Mouse(string   name,   Subject   sub)   :   base(sub)   
    38   {   
    39   this.name   =   name;   
    40   }   
    41   public   override   void   Response()   
    42   {   
    43   Console.WriteLine(name   +   "   attempt   to   escape! ");   
    44   }   
    45   }   
    46   public   class   Master   :   Observer   
    47   {   
    48   public   Master(Subject   sub)   :   base(sub){}   
    49   public   override   void   Response()   
    50   {   
    51   Console.WriteLine( "host   waken ");   
    52   }   
    53   }   
    54 
    55   class   MainClass   
    56   {   
    57   /**////   <summary>   
    58   ///   应用程序的主入口点。   
    59   ///   </summary>   
    60   [STAThread]   
    61   static   void   Main(string[]   args)   
    62   {   
    63   Cat   cat   =   new   Cat();   
    64   Mouse   mouse1   =   new   Mouse( "mouse1 ",   cat);   
    65   Mouse   mouse2   =   new   Mouse( "mouse2 ",   cat);   
    66   Master   master   =   new   Master(cat);   
    67   cat.Cry();   
    68   }   
    69   }   
    70} 
    看看这个
      

  2.   

       public void CatCry() 
            { 
                Console.WriteLine("Cat Cry!"); 
                this.EventShouter += new CatShouter(mouseRun); 
                this.EventShouter ();
            }