前言:
今天去面试的时候,面试官问到C#中委托和事件的使用,以及你在游戏中会怎么使用它们。这个问题给了我一个启发:在学习新的技术点时,一定要多考虑该如何具体使用它们,而不是一味的学习知识点,正所谓:学以致用。
当然由于我的水平有限,一些观点可能存在错误,非常欢迎大家可以指正我的错误。
知识点:
- C# 中的委托(Delegate)类似于 C 或 C++ 中函数指针。我觉得这句话可以帮助我们透彻简炼地理解委托:说白了委托就是一个封装成类的函数指针,这个类可以实例化并且实例可以添加和删除指针。
- 回调函数:函数指针作为某个函数的参数时,其就可以被称为回调函数。
- C#中的事件是用委托实现的。(事件监听:监听委托(指针)是否为空,若不为空触发事件?)
下面我们来看委托和事件:
C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。
委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。C# 事件(Event)
事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些出现,如系统生成的通知。应用程序需要在事件发生时响应事件。例如,中断。事件是用于进程间通信。
通过事件使用委托
事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联。包含事件的类用于发布事件。这被称为 发布器(publisher) 类。其他接受该事件的类被称为 订阅器(subscriber) 类。事件使用 发布-订阅(publisher-subscriber) 模型。
发布器(publisher) 是一个包含事件和委托定义的对象。事件和委托之间的联系也定义在这个对象中。发布器(publisher)类的对象调用这个事件,并通知其他的对象。
订阅器(subscriber) 是一个接受事件并提供事件处理程序的对象。在发布器(publisher)类中的委托调用订阅器(subscriber)类中的方法(事件处理程序)。声明事件(Event)
在类的内部声明事件,首先必须声明该事件的委托类型。例如:
public delegate void BoilerLogHandler(string status);
然后,声明事件本身,使用 event 关键字:
// 基于上面的委托定义事件
public event BoilerLogHandler BoilerEventLog;
上面的代码定义了一个名为 BoilerLogHandler 的委托和一个名为 BoilerEventLog 的事件,该事件在生成的时候会调用委托。
// -----------------------------1-------------------------------
// 函数指针的实例
#include <stdio.h>
int max(int x, int y)
{
return x > y ? x : y;
}
int main(void)
{
/* p 是函数指针 */
int (* p)(int, int) = & max; // &可以省略
int a, b, c, d;
printf("请输入三个数字:");
scanf("%d %d %d", & a, & b, & c);
/* 与直接调用函数等价,d = max(max(a, b), c) */
d = p(p(a, b), c);
printf("最大的数字是: %d\n", d);
return 0;
}
// --------------------- 2 --------------------------------
// 回调函数的实例
#include <stdlib.h>
#include <stdio.h>
// 回调函数
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
for (size_t i=0; i<arraySize; i++)
array[i] = getNextValue();
}
// 获取随机值
int getNextRandomValue(void)
{
return rand();
}
int main(void)
{
int myarray[10];
populate_array(myarray, 10, getNextRandomValue);
for(int i = 0; i < 10; i++) {
printf("%d ", myarray[i]);
}
printf("\n");
return 0;
}
// -------------------------------------3---------------------------
// 事件
using System;
namespace SimpleEvent
{
using System;
/***********发布器类***********/
public class EventTest
{
private int value;
public delegate void NumManipulationHandler(); // 一个委托类型
public event NumManipulationHandler ChangeNum; // 使用上述委托实现的事件
protected virtual void OnNumChanged() // OnXXX 像不像android开发中的监听?
{
if ( ChangeNum != null )
{
ChangeNum(); /* 事件被触发 */
}else {
Console.WriteLine( "event not fire" );
Console.ReadKey(); /* 回车继续 */
}
}
public EventTest()
{
int n = 5;
SetValue( n );
}
public void SetValue( int n )
{
if ( value != n )
{
value = n;
OnNumChanged();
}
}
}
/***********订阅器类***********/
public class subscribEvent
{
public void printf()
{
Console.WriteLine( "event fire" );
Console.ReadKey(); /* 回车继续 */
}
}
/***********触发***********/
public class MainClass
{
public static void Main()
{
EventTest e = new EventTest(); /* 实例化对象,第一次没有触发事件 */
subscribEvent v = new subscribEvent(); /* 实例化对象 */
e.ChangeNum += new EventTest.NumManipulationHandler( v.printf ); /* 注册 */
// 上边语句,运行时注册,像不像多态?
e.SetValue( 7 );
e.SetValue( 11 );
}
}
}
//-----------------------------------------4---------------------------------
// 这个例子是引用3书中的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Example_17_2_ _ _ _Delegates_and_Events
{
// a class to hold the information about the event
// in this case it will hold only information
// available in the clock class, but could hold
// additional state information
public class TimeInfoEventArgs : EventArgs
{
public int hour;
public int minute;
public int second;
public TimeInfoEventArgs(int hour, int minute, int second)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
}
// The publisher: the class that other classes
// will observe. This class publishes one delegate:
// SecondChangeHandler.
public class Clock
{
private int hour;
private int minute;
private int second;
// the delegate the subscribers must implement
public delegate void SecondChangeHandler(object clock,
TimeInfoEventArgs timeInformation);
// an instance of the delegate
public SecondChangeHandler SecondChanged;
// set the clock running
// it will raise an event for each new second
// 事件监听/事件触发条件
public void Run( )
{
for (; ; )
{
// sleep 100 milliseconds
Thread.Sleep(100);
// get the current time
System.DateTime dt = System.DateTime.Now;
// if the second has changed
// notify the subscribers
if (dt.Second != second)
{
// create the TimeInfoEventArgs object
// to pass to the subscriber
TimeInfoEventArgs timeInformation =
new TimeInfoEventArgs(dt.Hour, dt.Minute, dt.Second);
// if anyone has subscribed, notify them
if (SecondChanged != null)
{
SecondChanged(this, timeInformation);
}
}
// update the state
this.second = dt.Second;
this.minute = dt.Minute;
this.hour = dt.Hour;
}
}
}
// A subscriber: DisplayClock subscribes to the
// clock's events. The job of DisplayClock is
// to display the current time
public class DisplayClock
{
// given a clock, subscribe to
// its SecondChangeHandler event
// 订阅
public void Subscribe(Clock theClock)
{
theClock.SecondChanged +=
new Clock.SecondChangeHandler(TimeHasChanged);
}
// the method that implements the
// delegated functionality
public void TimeHasChanged(object theClock, TimeInfoEventArgs ti)
{
Console.WriteLine("Current Time: {0}:{1}:{2}",
ti.hour.ToString( ), ti.minute.ToString( ), ti.second.ToString( ));
}
}
// a second subscriber whose job is to write to a file
public class LogCurrentTime
{
// 订阅
public void Subscribe(Clock theClock)
{
theClock.SecondChanged +=
new Clock.SecondChangeHandler(WriteLogEntry);
}
// this method should write to a file
// we write to the console to see the effect
// this object keeps no state
public void WriteLogEntry(object theClock, TimeInfoEventArgs ti)
{
Console.WriteLine("Logging to file: {0}:{1}:{2}",
ti.hour.ToString( ), ti.minute.ToString( ), ti.second.ToString( ));
}
}
public class Tester
{
public void Run( )
{
// create a new clock
Clock theClock = new Clock( );
// create the display and tell it to
// subscribe to the clock just created
DisplayClock dc = new DisplayClock( );
dc.Subscribe(theClock);
// create a Log object and tell it
// to subscribe to the clock
LogCurrentTime lct = new LogCurrentTime( );
lct.Subscribe(theClock);
// Get the clock started
theClock.Run( );
}
}
public class Program
{
public static void Main( )
{
Tester t = new Tester( );
t.Run( );
}
}
}
总结:
在面试的时候,面试官让我思考如何在自己完成的项目中使用委托。虽然在提示下,我也答出了一些,但思路不是很简练明晰。
Q:如果想给牧师添加一些其他类中实现的特效?
A:
1. 使用委托。我可以在牧师类中定义一个委托类,然后声明一个委托实例。这样在场景控制器中,通过向委托实例中添加具有相同签名的特效函数,这样我就可以调用牧师的特效监听函数,当事件被触发后,监听函数中可以调用其他类中实现的特效函数。而且这样做,避免了硬编码,你可以在运行时调用不同的特效。
2. 使用接口。对接口编程不对实现编程。我也可以在牧师类中声明一个接口实例,然后在战斗子系统中,针对玩家状态,给接口引用不同的特效,然后调用牧师的特效展示方法,在牧师自身的特效展示方法中,调用引用的特效方法。
订阅发布模式的另类记忆:
A对X:我把身家性命交给你了,有事通知我/有事随便用
B对X:我把身家性命交给你了,有事通知我/有事随便用
事情来了….
X:使用A的身家性命,使用B的身家性命。
那么…另外一个问题来了:啥时候用委托?啥时候用接口呢?具体MSDN上有,长长的一篇文章。
引用:
[1]函数指针和回调函数:菜鸟论坛
[2]委托和事件:菜鸟论坛
[3]Delegates and Events: MSDN上推荐的书里的一个章节,国外书中有很多有趣的类比例子。里边还讲到了匿名函数和lambda表达式在委托和事件中的应用。