C#中的事件是指某个类的对象在运行过程中遇到的一些特定事情,而这些特定的事情有必要通知给这个对象的使用者。当发生于某个对象相关的事件时,类会使用事件将这一对象通知给用户,这这种通知称为“引发事件”。引发事件的对象称为事件的源或者发送者。对象引发事件的原因很多,相应对象数据的更改、长时间运行的进程完成或者服务中断等。
委托的发布和订阅
由于委托能够引用方法,而且能够连接和删除其他委托对象,因而就能够通过委托来实现事件的发布和订阅,这两个必要的过程,通过委托来实现事件处理的过程,通常需要4个步骤。
综合练习-通过委托使学生对铃声做出响应
完整示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegateRelease
{
internal class Program
{
static void Main(string[] args)
{
SchoolRing sr = new SchoolRing();//创建学校铃声类
Students student = new Students();//创建学生实例
student.SubscribeToRing(sr);//订阅铃声
Console.Write("请输入打铃参数(1:表示打上课铃;2:表示打下课铃):");
sr.Jow(Convert.ToInt32(Console.ReadLine()));//打铃动作
}
}
public delegate void RingEvent(int ringKind);//声明一个委托类型,参数ringKind表示铃声种类(1:表示上课铃声;2表示下课铃声)
public class SchoolRing
{
public RingEvent OnBellSound; //委托发布
public void Jow(int ringKind) //打铃
{
if (ringKind == 1 || ringKind == 2)
{
Console.Write(ringKind == 1 ? "上课铃声响了," : "下课铃声响了,");
if (OnBellSound != null)//不等于空,说明它已经订阅了具体的方法(即它已经引用了具体的方法)
{
OnBellSound(ringKind);//回调OnBellSound委托所订阅(或引用)的具体方法
}
}
else
{
Console.WriteLine("这个铃声参数不正确!");
}
}
}
public class Students
{
public void SubscribeToRing(SchoolRing schoolRing) //学生们订阅铃声这个委托事件
{
schoolRing.OnBellSound += SchoolJow;
}
public void SchoolJow(int ringKind)
{
if (ringKind == 2)//打了下课铃
{
Console.WriteLine("同学们开始课间休息!");
}
else if (ringKind == 1)//打了上课铃
{
Console.WriteLine("同学们开始认真学习!");
}
}
public void CancelSubscribe(SchoolRing schoolRing)//取消订阅铃声动作
{
schoolRing.OnBellSound -= SchoolJow;
}
}
}