实验四_委托的实现与使用_()

实验四_委托的实现与使用_()

ID:35342315

大小:108.43 KB

页数:7页

时间:2019-03-23

实验四_委托的实现与使用_()_第1页
实验四_委托的实现与使用_()_第2页
实验四_委托的实现与使用_()_第3页
实验四_委托的实现与使用_()_第4页
实验四_委托的实现与使用_()_第5页
资源描述:

《实验四_委托的实现与使用_()》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、验报告课程名称面向对象程序设计实验名称实验四、委托的实现与使用H期2015.4.28学生学号姓名班级实验目的:1.理解委托和事件的概念2.了解委托和事件在C#中的实现实验要求:1•认真阅读、掌握和本实验相关的教材内容。2.设计并编写代码完成题FI要求的任务。3•撰写实验报告。实验内容与步骤:编写一个程序用来模拟考试流程,有一个老师类Teacher,学生类Student,老师引发考试事件,学生进行考试,学生答完题发出完成答题事件,老师收卷。1)创建一个名为DelegateEvent的控制台项02)添加Student类,代码如下,要注意对toString方法的

2、重载,Studen啖继承自0bject类,Objec啖的默认toString方法返冋的是命名空间和类名,Studen啖返冋Student的成员变量name,这样使设计的类更可用。.NETFramework类库大多数类都对toString方法进行了重载usingSystem;usingSystem・Collections.Generic;usingSystem.Text;namespaceDelegateEvent{//定义委托publicdelegatevoiddelegateFinishExamg.teTimeFinishiTime,Studentstu

3、dent);publicclassStudent{〃用委托定义事件publiceventdelegateFinishExamFinishExam;privatestringname;publicStudent(stringname){this.name=name;}〃答题publicvoidTesting(DateTimeBeginTime){Console.WriteLine(“{0}t{1}t开始答题・・・"、name,BeginTime);}〃答题结束,引发考试完成事件publicvoidHandlnPaper(){Console.WriteLi

4、ne(“{0}答题完成〃,name);FinishExam(DateTinie・Now,this);}publicoverridestringToStringOreturnthis.name;3)添加Teacher类,代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceDelegateEvent{publicdelegatevoidDelegateStartExam(DateTimeStartTime);publicclassTeacher{publiceve

5、ntDelegateStartExamStartExam;publicvoidNotifyBeginExam(){StartExam(DateTime・Now);//收卷publicvoidAcceptPaper(DateTimeacceptTime,Studentstudent){Console.WriteLine(zz收{0}的考卷〃,student.ToString())a;}4)改写Program,cs的代码如下:usingSystem;usingSystem・Collections.Generic;usingSystem.Text;namespa

6、ceDelegateEvent{classProgram{staticvoidMain(string[]args)Teacherteacher=newTeacher();Student[]students=newStudent[5];intindex=0;students[index++]newStudent(〃张三〃);students[index++]newStudent(〃李四〃);students[index++]newStudent(〃王五〃);students[index++]newStudent(〃周六〃);students[index++]

7、newStudent(〃冯七〃);foreach(Studenttesterinstudents)//事件teacher.StartExam+二newDelegateStartExam(tester・Testing);tester・FinishExam+二newdelegateFinishExam(teacher・AcceptPaper);}〃引发开始答卷事件teacher・NotifyBeginExam();〃引发完成答卷事件students[1]・HandinPaper();5)按Ctrl+F5运行程序,并贴出实验结果。2015-4-282015-4-

8、282015-4-282015-4-2811:13:4711:13

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。