您的位置:首页 > 编程语言 > C#

一道有趣的C#考试题目

2008-08-23 22:50 561 查看
题目:猫叫,老鼠逃跑,主人惊醒。(10分)

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


程序代码
1using System;
2using System.Collections;
3
4namespace ConsoleApplication1
5{
6 public interface Observer
7 {
8 void Response(); //观察者的响应,如是老鼠见到猫的反映
9 }
10 public interface Subject
google_ad_client = "pub-4475724770859924";google_alternate_color = "FFBBE8";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel ="9379930647";google_color_border = "F8F8F8";google_color_bg = "FFFFFF";google_color_link = "FF6FCF";google_color_url = "38B63C";google_color_text = "B3B3B3";

11 {
12 void AimAt(Observer obs); //针对哪些观察者,这里指猫的要扑捉的对象---老鼠
13 }

14
15 public class Mouse : Observer
16 {
17 private string name;
18 public Mouse(string name, Subject subj)
19 {
20 this.name = name;
21 subj.AimAt(this);
22 }
23
24 public void Response()
25 {
26 Console.WriteLine(name + " attempt to escape!");
27 }
28 }
29
30 public class Master : Observer
31 {
32 public Master(Subject subj)
33 {
34 subj.AimAt(this);
35 }
36
37 public void Response()
38 {
39 Console.WriteLine("Host waken!");
40 }
41 }
42
43
44 public class Cat : Subject
45 {
46 private ArrayList observers;
47 public Cat()
48 {
49 this.observers = new ArrayList();
50 }
51 public void AimAt(Observer obs)
52 {
53 this.observers.Add(obs);
54 }
55 public void Cry()
56 {
57 Console.WriteLine("Cat cryed!");
58 foreach (Observer obs in this.observers)
59 {
60 obs.Response();
61 }
62 }
63 }
64
65 class MainClass
66 {
67 /**//// <summary>
68 /// 应用程序的主入口点。
69 /// </summary>
70 [STAThread]
71 static void Main(string[] args)
72 {
73 Cat cat = new Cat();
74 Mouse mouse1 = new Mouse("mouse1", cat);
75 Mouse mouse2 = new Mouse("mouse2", cat);
76 Master master = new Master(cat);
77 cat.Cry();
78 }
79 }
80}

--------------------------------------------------------------------------------

设计方法二: 使用event -- delegate设计..


程序代码
1using System;
2using System.Collections;
3
4
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}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: