程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。
要求: 
1.要有联动性,老鼠和主人的行为是被动的。
2.考虑可扩展性,猫的叫声可能引起其他联动效应。


要点:1. 联动效果,运行代码只要执行Cat.Cryed()方法。2. 对老鼠和主人进行抽象
评分标准: <1>.构造出Cat、Mouse、Master三个类,并能使程序运行(2分)
            <2>从Mouse和Master中提取抽象(5分)
            <3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。(3分)

 

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 
  5 namespace CMMClasses
  6 {
  7     public class OnCryedEventArgs : EventArgs
  8     {
  9         public OnCryedEventArgs(string s)
 10         {
 11             this.Message = s;
 12         }
 13 
 14         private string _message;
 15         public string Message
 16         {
 17             get { return this._message; }
 18             set { this._message = value; }
 19         }
 20     }
 21 
 22     public class Cat:IShout,ISomeThing
 23     {
 24         public event EventHandler<OnCryedEventArgs> OnCryed;
 25 
 26         private string _name;
 27         public string Name
 28         {
 29             get { return this._name; }
 30             set { this._name = value; }
 31         }
 32 
 33         public Cat(string s)
 34         {
 35             this.Name = s;
 36         }
 37 
 38         public void Cryed()
 39         {
 40             OnCryed(thisnew OnCryedEventArgs(this.Name + " Cryed!"));
 41         }
 42     }
 43 
 44     public interface ISomeThing
 45     {
 46         string Name{get;}
 47     }
 48 
 49     public class SomeWhere 
 50     {
 51         List<ISomeThing> list = new List<ISomeThing>();
 52 
 53         public SomeWhere() 
 54         {
 55             OnMakeSound += new EventHandler<OnCryedEventArgs>(SomeWhere_OnMakeSound);        
 56         }
 57 
 58         void SomeWhere_OnMakeSound(object sender, OnCryedEventArgs e)
 59         {
 60             foreach (ISomeThing st in list)
 61             {
 62                 if (st is IBeScared)
 63                 {
 64                     (st as IBeScared).BeScared();
 65                 }
 66             }            
 67         }
 68 
 69         void SomeWhere_OnCryed(object sender, OnCryedEventArgs e)
 70         {
 71             System.Web.HttpContext.Current.Response.Write((sender as ISomeThing).Name + "大叫!</br>");
 72             OnMakeSound(sender, e);
 73         }
 74 
 75         public void Add(ISomeThing st)
 76         {
 77             list.Add(st);
 78 
 79             if (st is IShout)
 80             {
 81                 (st as Cat).OnCryed += new EventHandler<OnCryedEventArgs>(SomeWhere_OnCryed);
 82             }
 83         }
 84 
 85         public event EventHandler<OnCryedEventArgs> OnMakeSound;
 86     }
 87 
 88     public interface IShout
 89     {
 90         void Cryed();
 91     }
 92 
 93     public class Master:IBeScared,ISomeThing
 94     {
 95         private string _name;
 96         public string Name
 97         {
 98             get { return this._name; }
 99             set { this._name = value; }
100         }
101 
102         public Master(string name)
103         {
104             this.Name = name;
105         }
106 
107         private void WakeUp() 
108         {
109             System.Web.HttpContext.Current.Response.Write(this.Name+"醒了!</br>");
110         }
111         public void BeScared()
112         {
113             WakeUp();
114         }
115     }
116 
117     public class Mouse : IBeScared,ISomeThing
118     {
119         private string _name;
120         public string Name
121         {
122             get { return this._name; }
123             set { this._name = value; }
124         }
125 
126         public Mouse(string name) {
127             this.Name = name;
128         }
129 
130         private void Flee()
131         {
132             System.Web.HttpContext.Current.Response.Write(this.Name+"逃跑了!</br>");        
133         }
134         public void BeScared()
135         {
136             Flee();
137         }
138     }
139 
140     public interface IBeScared
141     {
142         void BeScared();
143     }
144 }
145 
 posted on 2009-03-11 17:58  weldon  阅读(123)  评论(0编辑  收藏  举报