1、C#面向对象程序设计训练福建工程学院信息科学与工程学院实验报告 2013 2014 学年第 一 学期 任课老师: 王晨阳 课程名称C#程序设计班级信管1102座号20姓名郭明光实验题目C#面向对象程序设计训练实验时间2013/10/24实验目的、内容3.设计题阅读下面说明和代码,在(n)处填充代码。(1)【说明】单件模式(Singleton)在某种程度上来说是限制而不是促进类的创建。单件模式确保类有且仅有一个实例,并提供了一个对该实例的全局访问点。在实际程序中,有很多类是需要确保有且仅有一个实例的。【代码】using System;class SingletonDemo private sta
2、tic SingletonDemo theSingleton = null; private (1)SingletonDemo () public (2)static SingletonDemo Instance() /静态全局 if (null = theSingleton) theSingleton = (3) new SingletonDemo() ; return theSingleton; static void Main(string args) SingletonDemo s1 = SingletonDemo.Instance(); SingletonDemo s2 = Sing
3、letonDemo.Instance(); if (s1.Equals(s2) Console.WriteLine(See, Only One Instance!); 【程序输出结果】(2)【说明】某高校的部门组织结构如图3-1所示,现采用组合(Composition)设计模式来设计,得到如图3-2所示的类图。其中Department为抽象类,定义了在组织结构图上添加(add)、删除(delete)部门和获取子部门列表的方法接口以及部门名称字段和封装部门名称字段的属性。类ConcreteDepartment表示具体的系部,各系部下还可以设置不同的子部门或教研室。类HRDepartment和类F
4、inanceDepartment分别表示人事处和财务处。简述组合(Composition)设计模式的特征?图3-1组织结构图图3-2类图【代码】using System;using System.Collections.Generic;using System.Text;namespace Composition public (1)abstract class Department protected string name; public string Name (2) get return this.name; public abstract void add(Department d)
5、; public abstract void delete(Department d); public abstract List getChildren(); public class ConcreteDepartment : Department private List children = new List(); public ConcreteDepartment(string name) this.name = name; public override void add(Department d) (3)children.add(d) ; public override void
6、delete(Department d) children.Remove(d); public override List getChildren() return this.children; public class HRDepartment : Department public HRDepartment(string name) this.name = name; public override void add(Department d) public override void delete(Department d) public override List getChildre
7、n() return null; public class FinanceDepartment : Department public FinanceDepartment(string name) this.name = name; public override void add(Department d) public override void delete(Department d) public override List getChildren() return null; class Program static void Display(Department departmen
8、t) Console.WriteLine(department.Name); if (department.getChildren()!=null) foreach (Department d in department.getChildren() Display(d); static void Main(string args) ConcreteDepartment college = new ConcreteDepartment(福建工程学院); college.add(new HRDepartment(人事处); ConcreteDepartment csDepartment = new
9、 ConcreteDepartment(计算机与信息科学系); csDepartment.add(new ConcreteDepartment(软件工程教研室); csDepartment.add(new ConcreteDepartment(信息管理教研室); ConcreteDepartment basicDepartment = new ConcreteDepartment(计算机基础教研室); basicDepartment.add(new ConcreteDepartment(计算机一级教研组); basicDepartment.add(new ConcreteDepartment(
10、计算机二级教研组); csDepartment.add(basicDepartment); college.add( (4)csDepartment ); college.add(new FinanceDepartment(财务处); Display( (5)college ); /打印整个学院各部门名称 【程序输出结果】(3)【说明】类UniversityList提供了只读的属性用于访问类中常量字段count;提供了索引器用于读取或设置类中数组型字段universityName,该索引器有个int型索引参数,在索引器中需判断该索引参数,如果超出09范围,则抛出一个异常,异常信息为“索引下标越
11、界”。【代码】using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1 public class UniversityList private const int count = 10; private string universityName = new stringcount; #region public int Count get return count; #endregion #region public strin
12、g thisint index get if (index = 0 & index = 0 & index = count) universityNameindex = value; else throw new Exception(索引下标越界); #endregion class Program static void Main(string args) UniversityList u = new UniversityList(); Console.WriteLine(u.Conut=0, u.Count); try u0 = 福建工程学院; Console.WriteLine(u0);
13、 u10 = 福州大学; Console.WriteLine(u0); catch (System.Exception ex) Console.WriteLine(ex.Message); 【程序输出结果】(4)【说明】以下程序中类Control发布了一个事件SomeEvent,该事件在用户按下键盘按键E时触发。【代码】using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1 public delegate void SomeHa
14、ndler(); public class Control /发布一个事件SomeEvent public event /*/ SomeHandler /*/ SomeEvent; public void RaiseSomeEvent() /触发事件SomeEvent /*/ if (this.SomeEvent != null) this.SomeEvent(); /*/ class Program public static void Main() Control ctrl = new Control(); /订阅事件SomeEvent /*/ ctrl.SomeEvent += new
15、SomeHandler(ProcessSomeEvent);/*/ Console.Write(请输入一个按键:); ConsoleKeyInfo cki = Console.ReadKey(); if (cki.Key = ConsoleKey.E) ctrl.RaiseSomeEvent(); private static void ProcessSomeEvent() Console.WriteLine(您好,按键E事件触发成功); 【程序输出结果】实验设计过程调试过程记录实验结果记录以及与预期结果比较以及分析总结以及心得体会指导老师评阅意见指导老师: 年 月 日填写内容时,可把表格扩大。实验的源程序代码(要有注释)附在表后。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1