一.委托(Delegate)
1.委托(Delegate)簡(jiǎn)介
最近天氣晴雨不定,你因?yàn)楹ε麓蛴螒驎r(shí)忘記在下雨時(shí)收衣服或者在天晴時(shí)把衣服掛出去,于是你委托好友小明在天氣發(fā)生變化時(shí)打電話通知你,這就是一種委托.
下面是這種委托的實(shí)例代碼
//定義一個(gè)繼承自Delegate類的子類小明,這之后小明就具備被委托的屬性
//public為公開,void為返回類型,XiaoMing為類名,(string msg)為傳遞給注冊(cè)的方法的參數(shù)
public delegate void XiaoMing(string msg);
class Me
{
//public代表聆聽小明的信號(hào)是公開的方法,以便小明訪問
//static是為了固定ListenToXiaoMing在內(nèi)存中的入口以便小明能夠找到該方法的入口
public static void ListenToXiaoMing(string msg)
{
if(msg == "Rain")
{
Console.WriteLine("下雨了!小明收衣服了!");
}
else if(msg == "Sunny")
{
Console.WriteLine("天晴了!小明曬衣服了!");
}
}
}
class Program
{
static void Main(string[] args)
{
//初始化一個(gè)XiaoMing類的實(shí)體xiaoMing
//將我的ListenToXiaoMing注冊(cè)在xiaoMing上
//相當(dāng)于我與小明約定下雨后小明就通知我做出行動(dòng)(調(diào)用ListenToXiaoMing)
XiaoMing xiaoMing = Me.ListenToXiaoMing;
//假設(shè)今天下雨
string weather = "Rain";
//小明觀察天氣
//將weather參數(shù)傳遞給Me.ListenToXiaoMing方法
xiaoMing.Invoke(weather);
}
}
以上代碼的輸出為
下雨了!小明收衣服了!
值得一提的是,如下代碼為創(chuàng)建一個(gè)繼承自Delegate類的名為XiaoMing的子類,該子類實(shí)例只接收void類型方法并且參數(shù)類型為(string)的注冊(cè),如:void foo(string test){}
public delegate void XiaoMing(string msg);
2.組播委托
你感覺小明的提醒十分靠譜,于是將這件事告訴了同樣喜歡玩游戲的小剛,于是小剛也決定請(qǐng)小明在天氣發(fā)生變化時(shí)提醒他,這就是組播委托.
當(dāng)執(zhí)行delegate.Invoke()時(shí),將會(huì)執(zhí)行所有注冊(cè)在該delegate上的方法
以下是這種委托的代碼:
public delegate void XiaoMing(string msg);
//你的類
class Me
{
//public代表聆聽小明的信號(hào)是公開的方法,以便小明訪問
//static是為了固定ListenToXiaoMing在內(nèi)存中的入口以便小明能夠找到該方法的入口
public static void MeListenToXiaoMing(string msg)
{
if(msg == "Rain")
{
Console.WriteLine("下雨了!小明收衣服了!");
}
else if(msg == "Sunny")
{
Console.WriteLine("天晴了!小明曬衣服了!");
}
}
}
//小剛的類
class XiaoGang
{
public static void XiaoGangListenToXiaoMing(string msg)
{
if (msg == "Rain")
{
Console.WriteLine("下雨了!小剛收衣服了!");
}
else if (msg == "Sunny")
{
Console.WriteLine("天晴了!小剛曬衣服了!");
}
}
}
class Program
{
static void Main(string[] args)
{
//初始化一個(gè)XiaoMing類的實(shí)體xiaoMing
//將我的ListenToXiaoMing注冊(cè)在xiaoMing上
//相當(dāng)于我與小明約定下雨后小明就通知我做出行動(dòng)(調(diào)用ListenToXiaoMing)
XiaoMing xiaoMing = Me.MeListenToXiaoMing;
//小剛也將自己的XiaoGangListenToXiaoMing注冊(cè)到被委托人小明身上
xiaoMing += XiaoGang.XiaoGangListenToXiaoMing;
//假設(shè)今天下雨
string weather = "Rain";
//小明觀察天氣
//將weather參數(shù)傳遞給Me.ListenToXiaoMing方法
//和XiaoGang.XiaoGangListenToXiaoMing方法
xiaoMing.Invoke(weather);
}
}
運(yùn)行結(jié)果為
下雨了!小明收衣服了!
下雨了!小剛收衣服了!
注意事項(xiàng):
在使用Delegate子類時(shí),需要用如下代碼進(jìn)行初始化
//方法一
XiaoMing xiaoMing;
xiaoMing=Me.MeListenToXiaoMing;
//方法二
XiaoMing xiaoMing=Me.MeListenToXiaoMing;
//錯(cuò)誤示范
XiaoMing xiaoMing;
xiaoMing+=Me.MeListenToXiaoMing;
3.事件(event)與委托配合
當(dāng)你和小剛為小明的靠譜感嘆時(shí),天氣變化無常的季節(jié)結(jié)束了,你們不再需要小明通知你們收放衣服了.
你們又有了新的需求----你喜歡在陰天運(yùn)動(dòng),但是小剛喜歡在晴天運(yùn)動(dòng),于是你們請(qǐng)小明吃了一頓飯,請(qǐng)求他在陰天通知你運(yùn)動(dòng),在晴天通知小剛運(yùn)動(dòng),這樣的話你們就不用在收到陰天和晴天的消息時(shí)花費(fèi)精力自行判斷要不要運(yùn)動(dòng)了.
以下是這次委托的代碼,小明將會(huì)根據(jù)不同情況通知不同的人
//好朋友小明類
class XiaoMing
{
//定義一個(gè)繼承自Delegate類的委托子類,就像是呼叫是人們通訊的方法
//而小明的呼叫是繼承自呼叫的子類
public delegate void CallTFirends();
//以下是事件,是一個(gè)用于觸發(fā)不同委托的字段
//這是委托你去運(yùn)動(dòng)的字段
public event CallTFirends CallMe;
//這是委托小剛?cè)ミ\(yùn)動(dòng)的字段
public event CallTFirends CallXiaoGang;
//好心的小明用下面的方法時(shí)時(shí)關(guān)注天氣
public void LookForWeather(string weather)
{
if (weather == "Cloudy")
{
//與下方的CallXiaoMing?.Invoke();具有相同功能
//即防止小明小剛還沒通知喜歡哪種運(yùn)動(dòng)天氣時(shí)進(jìn)行提醒
//防止在event還沒注冊(cè)時(shí)就被調(diào)用
if (CallMe != null)
{
CallMe.Invoke();
}
}
else if(weather == "Sunny")
{
CallXiaoGang?.Invoke();
}
}
}
//你的類
class Me
{
//你收到小明的信號(hào)會(huì)調(diào)用以下方法去運(yùn)動(dòng)
public static void MeListenToXiaoMing()
{
Console.WriteLine("小明運(yùn)動(dòng)啦!");
}
}
//小剛的類
class XiaoGang
{
//小剛收到小明的信號(hào)會(huì)調(diào)用以下方法去運(yùn)動(dòng)
public static void XiaoGangListenToXiaoMing()
{
Console.WriteLine("小剛運(yùn)動(dòng)啦!");
}
}
class Program
{
static void Main(string[] args)
{
XiaoMing xiaoMing = new XiaoMing();
//天氣為陰天
string weather = "Cloudy";
//你注冊(cè)了callMe的事件,意味通知小明在陰天通知你去運(yùn)動(dòng)
xiaoMing.CallMe += Me.MeListenToXiaoMing;
//小剛注冊(cè)了XiaoGang.XiaoGangListenToXiaoMing事件,意味通知小明在晴天通知小剛?cè)ミ\(yùn)動(dòng)
xiaoMing.CallXiaoGang += XiaoGang.XiaoGangListenToXiaoMing;
//小明觀察天氣并且提醒對(duì)應(yīng)的人去運(yùn)動(dòng)
xiaoMing.LookForWeather(weather);
}
}
運(yùn)行結(jié)果為
小明運(yùn)動(dòng)啦!
細(xì)節(jié)提示:
1.事件只能在聲明它的類中被觸發(fā)(即例子中在XiaoMing類種才能觸發(fā)),在聲明它的類之外只能訂閱和取消訂閱。文章來源:http://www.zghlxwxcb.cn/news/detail-411027.html
2.事件的一大作用是方便由聲明Delegate的類集中管理而不用每個(gè)類自行判斷傳輸過來的信號(hào),如你和小剛不用自行判斷傳來的天氣適合做什么只需要在收到消息后去運(yùn)動(dòng)即可.文章來源地址http://www.zghlxwxcb.cn/news/detail-411027.html
到了這里,關(guān)于C#中的委托(Delegate)和事件 (Event)詳解與使用范例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!