一道有趣的C#考试题目

一道有趣的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}


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

设计方法二: 使用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} 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值