委托和事件练习之-学校考试
就委托和事件,尝试一个作业,可参考前面的《老鼠出现引发的血案》。
这次我们就学生考试展开:老师下令考试,学生开始考试;学生宣布考完,老师就来收卷。怎么写?
using System;
namespace 委托与事件总结
{
//定义委托
public delegate void delegateFinishedExam(DateTime FinishedTime, Student student);
public delegate void delegateStartExam(DateTime StartTime);
public class Student
{
//定义事件,关联到委托
public event delegateFinishedExam EventFinishExam;
private string _name;
//在构造中初始化成员(字段)变量
public Student(string name)
{
this._name = name;
}
//答题的函数,开始答题
public void BeginExam(DateTime BeginTime)
{
Console.WriteLine("\n时间为:" + BeginTime + " " + _name + "开始答题:...");
}
//答题完,引发完成考试事件
public void EndExam()
{
Console.WriteLine();
Console.WriteLine(this.ToString() + "答题完成.");
EventFinishExam(DateTime.Now, this);
// EventFinishExam(this);
}
//重载基类的ToString方法,让ToString方法返回成员变量_name的值,即学生姓名
public override string ToString()
{
return this._name;
}
}
public class Teacher
{
//定义事件
public event delegateStartExam EventStartExam;
public Teacher() { }
//引发开始测试事件的方法
public void NotifyBeginExam()
{
EventStartExam(DateTime.Now);
}
//收考卷的方法.此处方法若写在入口类,须声明为静态方法
public void AcceptPaper(DateTime AcceptTime, Student stu)
{
Console.WriteLine("\n收到{0} {1}的考卷.", stu.ToString(), AcceptTime);
}
}
class RunApplication
{
static void Main(string[] args)
{
Teacher teacher = new Teacher();
Student[] students = new Student[5];
int i = 0;
students[i++] = new Student("尹一");
students[i++] = new Student("安二");
students[i++] = new Student("张三");
students[i++] = new Student("李四");
students[i--] = new Student("王五");
foreach (Student student in students)
{
//给每一个stus(学生)对象订阅tea(老师)对象的EventStartExam事件
teacher.EventStartExam += new delegateStartExam(student.BeginExam);
//给tea(老师)对象订阅每一个学生的完成答卷事件
student.EventFinishExam += new delegateFinishedExam(teacher.AcceptPaper);
}
//tea(老师)对象通过函数NotifyBeginExam()引发EventStartExam事件
teacher.NotifyBeginExam();
Console.Write("请输入学生编号:");
int num = int.Parse(Console.ReadLine());
if (num < 0 || num > 4)
{
Console.WriteLine("您输入的编号非法!");
}
//stus[num]对象通过函数HandInPaper()引发EventFinishExam事件,完成考试
students[num].EndExam();
Console.ReadLine();//交给控制台(不调试)
}
}
}
namespace 委托与事件总结
{
//定义委托
public delegate void delegateFinishedExam(DateTime FinishedTime, Student student);
public delegate void delegateStartExam(DateTime StartTime);
public class Student
{
//定义事件,关联到委托
public event delegateFinishedExam EventFinishExam;
private string _name;
//在构造中初始化成员(字段)变量
public Student(string name)
{
this._name = name;
}
//答题的函数,开始答题
public void BeginExam(DateTime BeginTime)
{
Console.WriteLine("\n时间为:" + BeginTime + " " + _name + "开始答题:...");
}
//答题完,引发完成考试事件
public void EndExam()
{
Console.WriteLine();
Console.WriteLine(this.ToString() + "答题完成.");
EventFinishExam(DateTime.Now, this);
// EventFinishExam(this);
}
//重载基类的ToString方法,让ToString方法返回成员变量_name的值,即学生姓名
public override string ToString()
{
return this._name;
}
}
public class Teacher
{
//定义事件
public event delegateStartExam EventStartExam;
public Teacher() { }
//引发开始测试事件的方法
public void NotifyBeginExam()
{
EventStartExam(DateTime.Now);
}
//收考卷的方法.此处方法若写在入口类,须声明为静态方法
public void AcceptPaper(DateTime AcceptTime, Student stu)
{
Console.WriteLine("\n收到{0} {1}的考卷.", stu.ToString(), AcceptTime);
}
}
class RunApplication
{
static void Main(string[] args)
{
Teacher teacher = new Teacher();
Student[] students = new Student[5];
int i = 0;
students[i++] = new Student("尹一");
students[i++] = new Student("安二");
students[i++] = new Student("张三");
students[i++] = new Student("李四");
students[i--] = new Student("王五");
foreach (Student student in students)
{
//给每一个stus(学生)对象订阅tea(老师)对象的EventStartExam事件
teacher.EventStartExam += new delegateStartExam(student.BeginExam);
//给tea(老师)对象订阅每一个学生的完成答卷事件
student.EventFinishExam += new delegateFinishedExam(teacher.AcceptPaper);
}
//tea(老师)对象通过函数NotifyBeginExam()引发EventStartExam事件
teacher.NotifyBeginExam();
Console.Write("请输入学生编号:");
int num = int.Parse(Console.ReadLine());
if (num < 0 || num > 4)
{
Console.WriteLine("您输入的编号非法!");
}
//stus[num]对象通过函数HandInPaper()引发EventFinishExam事件,完成考试
students[num].EndExam();
Console.ReadLine();//交给控制台(不调试)
}
}
}