惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
B
Blog
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
IT之家
IT之家
D
Docker
Google DeepMind News
Google DeepMind News
罗磊的独立博客
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta

博客园 - CrazyWill

VBscript(ASP)计算程序运行时间,精度到毫秒 VBscript(ASP)常用函数 301永久重定向实现方式及302重定向 开源搜索引擎资源 18种方法让你集中精力工作 IE的BUG - CrazyWill - 博客园 弱智的卡巴死机 bash比较文件时间和当前时间 - CrazyWill - 博客园 老问题: SQL server 2000安装过程中错误解决 使用TELNET手工操作 IMAP 查看邮件 安装 samba 记录 使用TELNET手工操作 SMTP/POP 收发邮件 SQL语句实现横排 2 世界 Web 2.0 网站评奖揭晓 MagicAjax使用简介 [链接]什么是Web 2.0——下一代软件的设计模式和商业模式 SQL语句实现合并数据(原创) SQL语句实现横排 WEB页面统一认证登录方法
一道有趣的C#考试题目
CrazyWill · 2005-11-07 · via 博客园 - CrazyWill

题目:猫叫,老鼠逃跑,主人惊醒。(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


设计方法二: 使用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}