[转载]两道程序设计题

 文章来源:http://www.cnblogs.com/lxinxuan/archive/2007/06/18/788299.html

 

题目:程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)

要求:
1.要有联动性,老鼠和主人的行为是被动的。

2.考虑可扩展性,猫的叫声可能引起其他联动效应

设计方法一:
None.gif public   interface  Observer 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
void Response(); //观察者的响应,如是老鼠见到猫的反映 
ExpandedBlockEnd.gif
}
 
None.gif
public   interface  Subject 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
void AimAt(Observer obs); //针对哪些观察者,这里指猫的要扑捉的对象---老鼠 
ExpandedBlockEnd.gif
}
 
None.gif
public   class  Mouse : Observer 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
private string name; 
InBlock.gif
public Mouse(string name, Subject subj) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
this.name = name; 
InBlock.gifsubj.AimAt(
this); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public void Response() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifConsole.WriteLine(name 
+ " attempt to escape!"); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
public   class  Master : Observer 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
public Master(Subject subj) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifsubj.AimAt(
this); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public void Response() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifConsole.WriteLine(
"Host waken!"); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif
public   class  Cat : Subject 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
private ArrayList observers; 
InBlock.gif
public Cat() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
this.observers = new ArrayList(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public void AimAt(Observer obs) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
this.observers.Add(obs); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public void Cry() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifConsole.WriteLine(
"Cat cryed!"); 
InBlock.gif
foreach (Observer obs in this.observers) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifobs.Response(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
class  MainClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifCat cat 
= new Cat(); 
InBlock.gifMouse mouse1 
= new Mouse("mouse1", cat); 
InBlock.gifMouse mouse2 
= new Mouse("mouse2", cat); 
InBlock.gifMaster master 
= new Master(cat); 
InBlock.gifcat.Cry(); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 

设计方法二: 使用event -- delegate设计
None.gif class  Class1
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Cat cat 
= new Cat();
InBlock.gif            Mouse mouse 
= new Mouse(cat);
InBlock.gif            Master master 
= new Master(cat);
InBlock.gif
InBlock.gif            cat.Cry();
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   delegate   void  SubEventHandle();
None.gif    
public   class  SubObject
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif        
public event SubEventHandle SubEvent;
InBlock.gif
InBlock.gif        
public void FireEvent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.SubEvent();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   class  Cat : SubObject
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
public void Cry()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.Write(
"Cat Cries.");
InBlock.gif            
this.FireEvent();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   abstract   class  Observer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public Observer(SubObject subObj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            subObj.SubEvent 
+= new SubEventHandle(Response);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public abstract void Response();
ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   class  Mouse : Observer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public Mouse(SubObject subObj)
InBlock.gif            : 
base(subObj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Response()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.Write(
"Mouse escapes.");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   class  Master : Observer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public Master(Cat cat)
InBlock.gif            : 
base(cat)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override void Response()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.Write(
"Master Weaks.");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

2、以下输出结果?
None.gif      public   class  A
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public virtual void Fun1(int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(i);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Fun2(A a)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            a.Fun1(
1);
InBlock.gif            Fun1(
5);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif    
public   class  B : A
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public override void Fun1(int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.Fun1(i + 1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            B b 
= new B();
InBlock.gif            A a 
= new A();
InBlock.gif            a.Fun2(b);
InBlock.gif            b.Fun2(a);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
答案:
2
5
1
6

3、以下输出结果?
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            test t1 
= new test();
InBlock.gif            test t2 
= new test();
InBlock.gif            Console.Write(t2.Count);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static int count = 0;
InBlock.gif
InBlock.gif        
static test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            count
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            count
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Count
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn count; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
答案:3

转载于:https://www.cnblogs.com/rangeliu/archive/2007/06/18/1188033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值