博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
两道程序设计题,算是备案吧
阅读量:5961 次
发布时间:2019-06-19

本文共 2722 字,大约阅读时间需要 9 分钟。

题目:程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(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
你可能感兴趣的文章
android textview字体加下划线
查看>>
springMVC 定时任务
查看>>
Mint8(ubuntu16.04) 搭建微信Web开发工具
查看>>
PostgreSQL数据类型-数据类型简介和布尔类型
查看>>
PostgreSQL数据类型-二进制数据和字符串数据类型与字符串函数
查看>>
安装应用的时候拷贝一个DB文件到应用database下
查看>>
shell 基础
查看>>
twisted的LineReceiver的接口定义
查看>>
浅解用PHP实现MVC
查看>>
MySQL常用操作
查看>>
Yxcms网站管理系统安装
查看>>
字符串,链表,树
查看>>
Nginx错误日志(error_log)配置及信息详解
查看>>
Highcharts 学习笔记
查看>>
高性能python编程之协程
查看>>
PHP编译过程中常见错误信息的解决方法
查看>>
redis服务端及php客户端安装方法
查看>>
定时删除文件
查看>>
mysql主从同步
查看>>
springmvc 高级3 之 统一异常处理
查看>>