C# 事件(Event)
<h2><span style="font-family: 幼圆">事件(Event) </span></h2><p><span style="font-family: 幼圆; font-size: 15px">基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件。例如,中断。</span></p>
<p><span style="font-family: 幼圆; font-size: 15px">C# 中使用事件机制实现线程间的通信。</span></p>
<h2><span style="font-family: 幼圆">通过事件使用委托</span></h2>
<p><span style="font-family: 幼圆; font-size: 15px">事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联。包含事件的类用于发布事件。这被称为 <strong>发布器(publisher)</strong> 类。其他接受该事件的类被称为 <strong>订阅器(subscriber)</strong> 类。事件使用 <strong>发布-订阅(publisher-subscriber)</strong> 模型。</span></p>
<p><span style="font-family: 幼圆; font-size: 15px"><strong>发布器(publisher)</strong> 是一个包含事件和委托定义的对象。事件和委托之间的联系也定义在这个对象中。发布器(publisher)类的对象调用这个事件,并通知其他的对象。</span></p>
<p><span style="font-family: 幼圆; font-size: 15px"><strong>订阅器(subscriber)</strong> 是一个接受事件并提供事件处理程序的对象。在发布器(publisher)类中的委托调用订阅器(subscriber)类中的方法(事件处理程序)。</span></p>
<h2><span style="font-family: 幼圆">声明事件(Event)</span></h2>
<p><span style="font-family: 幼圆; font-size: 15px">在类的内部声明事件,首先必须声明该事件的委托类型。例如:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: 幼圆; font-size: 15px"><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">delegate</span> <span style="color: rgba(0, 0, 255, 1)">void</span> BoilerLogHandler(<span style="color: rgba(0, 0, 255, 1)">string</span> status);</span></pre>
</div>
<p><span style="font-family: 幼圆; font-size: 15px">然后,声明事件本身,使用 <strong>event</strong> 关键字:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: 幼圆; font-size: 15px"><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 基于上面的委托定义事件</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">event</span> BoilerLogHandler BoilerEventLog;</span></pre>
</div>
<p><span style="font-family: 幼圆; font-size: 15px">上面的代码定义了一个名为 BoilerLogHandler 的委托和一个名为 BoilerEventLog 的事件,该事件在生成的时候会调用委托。</span></p>
<h2><span style="font-family: 幼圆" data-ttu-id="a390d-119">事件和委托的联系与区别</span></h2>
<p><span style="font-family: 幼圆; font-size: 15px" data-ttu-id="a390d-119">从事件的声明,我们可以大致看出事件与委托的关系,事件是委托的特殊实现,事件是建立在对委托的语言支持之上的。</span></p>
<p><span style="font-family: 幼圆; font-size: 15px" data-ttu-id="a390d-119">委托是一种类型,事件是委托类型的一个实例,加上了event的权限控制,限制权限,只允许在事件声明类里面去invoke和赋值,不允许外面,甚至子类调用。</span></p>
<h2><span style="font-family: 幼圆">经典面试题—猫叫,主人醒,老鼠跑</span></h2>
<div class="cnblogs_code">
<pre><span style="font-family: 幼圆; font-size: 15px"><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;
</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> DelegateDemo
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义猫叫委托</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">delegate</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> CatCallEventHandler();
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Cat
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义猫叫事件</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)"> CatCallEventHandler CatCall;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnCatCall()
{
Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">猫叫了一声</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
CatCall</span>?<span style="color: rgba(0, 0, 0, 1)">.Invoke();
}
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Mouse
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义老鼠跑掉方法</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> MouseRun()
{
Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">老鼠跑了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> People
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义主人醒来方法</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> WakeUp()
{
Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">主人醒了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Program
{
</span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> Main(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
{
Cat cat </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Cat();
Mouse m </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Mouse();
People p </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> People();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关联绑定 </span>
cat.CatCall += <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CatCallEventHandler(m.MouseRun);
cat.CatCall </span>+= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CatCallEventHandler(p.WakeUp);
cat.OnCatCall();
Console.ReadKey();
}
}
}</span></span></pre>
</div>
<p><span style="font-family: 幼圆">运行结果:</span></p>
<p><span style="font-family: 幼圆"><img src="https://img2018.cnblogs.com/i-beta/1146926/202001/1146926-20200107173503432-1120383907.png" alt=""></span></p><br><br>
来源:https://www.cnblogs.com/zhaoyl9/p/12162697.html
頁:
[1]