一道有趣的C#考试题目
题目:猫叫,老鼠逃跑,主人惊醒。(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
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}
81
82
2using System.Collections;
3
4namespace ConsoleApplication1
5{
6 public interface Observer
7 {
8 void Response(); //观察者的响应,如是老鼠见到猫的反映
9 }
10 public interface Subject
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}
81
82
设计方法二: 使用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}
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}
posted on 2005-11-07 16:39 CrazyWill 阅读(2491) 评论(2) 编辑 收藏 举报