实验四_委托的实现与使用_().doc_第1页
实验四_委托的实现与使用_().doc_第2页
实验四_委托的实现与使用_().doc_第3页
实验四_委托的实现与使用_().doc_第4页
实验四_委托的实现与使用_().doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

验报告课程名称面向对象程序设计实验名称实验四、委托的实现与使用日期2015.4.28学生学号姓名班级实验目的:1理解委托和事件的概念2了解委托和事件在C#中的实现实验要求:1.认真阅读、掌握和本实验相关的教材内容。2.设计并编写代码完成题目要求的任务。3.撰写实验报告。实验内容与步骤:编写一个程序用来模拟考试流程,有一个老师类Teacher,学生类Student,老师引发考试事件,学生进行考试,学生答完题发出完成答题事件,老师收卷。1)创建一个名为DelegateEvent的控制台项目2)添加Student类,代码如下,要注意对toString方法的重载,Student类继承自Object类,Object类的默认toString方法返回的是命名空间和类名,Student类返回Student的成员变量name,这样使设计的类更可用。.NET Framework类库大多数类都对toString方法进行了重载using System;using System.Collections.Generic;using System.Text;namespace DelegateEvent /定义委托 public delegate void delegateFinishExam(DateTime FinishiTime, Student student); public class Student /用委托定义事件 public event delegateFinishExam FinishExam; private string name; public Student(string name) = name; /答题 public void Testing(DateTime BeginTime) Console.WriteLine(0t1t开始答题., name, BeginTime); /答题结束,引发考试完成事件 public void HandInPaper() Console.WriteLine(0答题完成, name); FinishExam(DateTime.Now, this); public override string ToString() return ; 3)添加Teacher类,代码如下:using System;using System.Collections.Generic;using System.Text;namespace DelegateEvent public delegate void DelegateStartExam(DateTime StartTime); public class Teacher public event DelegateStartExam StartExam; public void NotifyBeginExam() StartExam(DateTime.Now); /收卷 public void AcceptPaper(DateTime acceptTime, Student student) Console.WriteLine(收0的考卷, student.ToString()a; 4)改写Program.cs的代码如下:using System;using System.Collections.Generic;using System.Text;namespace DelegateEvent class Program static void Main(string args) Teacher teacher = new Teacher(); Student students = new Student5; int index = 0; studentsindex+ = new Student(张三); studentsindex+ = new Student(李四); studentsindex+ = new Student(王五); studentsindex+ = new Student(周六); studentsindex+ = new Student(冯七); foreach (Student tester in students) /事件 teacher.StartExam += new DelegateStartExam(tester.Testing); tester.FinishExam += new delegateFinishExam(teacher.AcceptPaper); /引发开始答卷事件 teacher.NotifyBeginExam(); /引发完成答卷事件 students1.HandInPaper(); 5)按Ctr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论