C#面向对象程序设计训练.docx

上传人:b****5 文档编号:30221087 上传时间:2023-08-07 格式:DOCX 页数:11 大小:131.22KB
下载 相关 举报
C#面向对象程序设计训练.docx_第1页
第1页 / 共11页
C#面向对象程序设计训练.docx_第2页
第2页 / 共11页
C#面向对象程序设计训练.docx_第3页
第3页 / 共11页
C#面向对象程序设计训练.docx_第4页
第4页 / 共11页
C#面向对象程序设计训练.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

C#面向对象程序设计训练.docx

《C#面向对象程序设计训练.docx》由会员分享,可在线阅读,更多相关《C#面向对象程序设计训练.docx(11页珍藏版)》请在冰豆网上搜索。

C#面向对象程序设计训练.docx

C#面向对象程序设计训练

福建工程学院信息科学与工程学院

实验报告

2013–2014学年第一学期任课老师:

王晨阳

课程名称

C#程序设计

班级

信管1102

座号

20

姓名

郭明光

实验题目

C#面向对象程序设计训练

实验时间

2013/10/24

实验目的、内容

3.设计题

阅读下面说明和代码,在(n)处填充代码。

(1)【说明】

单件模式(Singleton)在某种程度上来说是限制而不是促进类的创建。

单件模式确保类有且仅有一个实例,并提供了一个对该实例的全局访问点。

在实际程序中,有很多类是需要确保有且仅有一个实例的。

【代码】

usingSystem;

classSingletonDemo

{

privatestaticSingletonDemotheSingleton=null;

private

(1)SingletonDemo(){}

public

(2)staticSingletonDemoInstance()//静态全局

{

if(null==theSingleton)

{

theSingleton=(3)newSingletonDemo();

}

returntheSingleton;

}

staticvoidMain(string[]args)

{

SingletonDemos1=SingletonDemo.Instance();

SingletonDemos2=SingletonDemo.Instance();

if(s1.Equals(s2))

{

Console.WriteLine("See,OnlyOneInstance!

");

}

}

}

【程序输出结果】

(2)【说明】

某高校的部门组织结构如图3-1所示,现采用组合(Composition)设计模式来设计,得到如图3-2所示的类图。

其中Department为抽象类,定义了在组织结构图上添加(add)、删除(delete)部门和获取子部门列表的方法接口以及部门名称字段和封装部门名称字段的属性。

类ConcreteDepartment表示具体的系部,各系部下还可以设置不同的子部门或教研室。

类HRDepartment和类FinanceDepartment分别表示人事处和财务处。

简述组合(Composition)设计模式的特征?

图3-1组织结构图

图3-2类图

【代码】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceComposition

{

public

(1)abstractclassDepartment

{

protectedstringname;

publicstringName{

(2)get{returnthis.name;}}

publicabstractvoidadd(Departmentd);

publicabstractvoiddelete(Departmentd);

publicabstractListgetChildren();

}

publicclassConcreteDepartment:

Department

{

privateListchildren=newList();

publicConcreteDepartment(stringname){this.name=name;}

publicoverridevoidadd(Departmentd)

{

(3)children.add(d);

}

publicoverridevoiddelete(Departmentd)

{

children.Remove(d);

}

publicoverrideListgetChildren(){returnthis.children;}

}

publicclassHRDepartment:

Department

{

publicHRDepartment(stringname){this.name=name;}

publicoverridevoidadd(Departmentd){}

publicoverridevoiddelete(Departmentd){}

publicoverrideListgetChildren(){returnnull;}

}

publicclassFinanceDepartment:

Department

{

publicFinanceDepartment(stringname){this.name=name;}

publicoverridevoidadd(Departmentd){}

publicoverridevoiddelete(Departmentd){}

publicoverrideListgetChildren(){returnnull;}

}

classProgram

{

staticvoidDisplay(Departmentdepartment)

{

Console.WriteLine(department.Name);

if(department.getChildren()!

=null)

{

foreach(Departmentdindepartment.getChildren())

{

Display(d);

}

}

}

staticvoidMain(string[]args)

{

ConcreteDepartmentcollege=newConcreteDepartment("福建工程学院");

college.add(newHRDepartment("人事处"));

ConcreteDepartmentcsDepartment=newConcreteDepartment("计算机与信息科学系");

csDepartment.add(newConcreteDepartment("软件工程教研室"));

csDepartment.add(newConcreteDepartment("信息管理教研室"));

ConcreteDepartmentbasicDepartment=newConcreteDepartment("计算机基础教研室");

basicDepartment.add(newConcreteDepartment("计算机一级教研组"));

basicDepartment.add(newConcreteDepartment("计算机二级教研组"));

csDepartment.add(basicDepartment);

college.add((4)csDepartment);

college.add(newFinanceDepartment("财务处"));

Display((5)college);//打印整个学院各部门名称

}

}

}

【程序输出结果】

(3)【说明】

类UniversityList提供了只读的属性用于访问类中常量字段count;提供了索引器用于读取或设置类中数组型字段universityName,该索引器有个int型索引参数,在索引器中需判断该索引参数,如果超出[0~9]范围,则抛出一个异常,异常信息为“索引下标越界”。

【代码】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceConsoleApplication1

{

publicclassUniversityList

{

privateconstintcount=10;

privatestring[]universityName=newstring[count];

#region

publicintCount

{

get{returncount;}

}

 

#endregion

#region

publicstringthis[intindex]

{

get

{

if(index>=0&&index<=count)

returnuniversityName[index];

else

returnuniversityName[0];

}

set

{

if(index>=0&&index<=count)

universityName[index]=value;

else

thrownewException("索引下标越界");

}

}

#endregion

}

classProgram

{

staticvoidMain(string[]args)

{

UniversityListu=newUniversityList();

Console.WriteLine("u.Conut={0}",u.Count);

try

{

u[0]="福建工程学院";

Console.WriteLine(u[0]);

u[10]="福州大学";

Console.WriteLine(u[0]);

}

catch(System.Exceptionex)

{

Console.WriteLine(ex.Message);

}

}

}

}

【程序输出结果】

(4)【说明】

以下程序中类Control发布了一个事件SomeEvent,该事件在用户按下键盘按键‘E’时触发。

【代码】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceConsoleApplication1

{

publicdelegatevoidSomeHandler();

publicclassControl

{

//发布一个事件SomeEvent

publicevent/**/SomeHandler/**/SomeEvent;

publicvoidRaiseSomeEvent()

{

//触发事件SomeEvent

/**/

if(this.SomeEvent!

=null)

this.SomeEvent();/**/

}

}

classProgram

{

publicstaticvoidMain()

{

Controlctrl=newControl();

//订阅事件SomeEvent

/**/

ctrl.SomeEvent+=newSomeHandler(ProcessSomeEvent);/**/

Console.Write("请输入一个按键:

");

ConsoleKeyInfocki=Console.ReadKey();

if(cki.Key==ConsoleKey.E)

{

ctrl.RaiseSomeEvent();

}

}

privatestaticvoidProcessSomeEvent()

{

Console.WriteLine("您好,按键'E'事件触发成功");

}

}

}

 

【程序输出结果】

 

实验设计过程

 

调试过程记录

 

实验结果记录以及与预期结果比较以及分析

 

总结以及心得体会

 

指导老师评阅意见

指导老师:

年月日

填写内容时,可把表格扩大。

实验的源程序代码(要有注释)附在表后。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1