c#事件和委托
一、委托(Delegate)
1、定義
delegate是C#中的一種類型,它實(shí)際上是一個(gè)能夠持有對某個(gè)方法的引用的類。與其它的類不同,delegate類能夠
擁有一個(gè)簽名(signature),并且它"只能持有與它的簽名相匹配的方法的引用"。它所實(shí)現(xiàn)的功能與C/C++中的函數(shù)指針
十分相似。它允許你傳遞一個(gè)類A的方法m給另一個(gè)類B的對象,使得類B的對象能夠調(diào)用這個(gè)方法m。但與函數(shù)指針相比,
delegate有許多函數(shù)委托和事件在 .Net Framework中的應(yīng)用非常廣泛指針不具備的優(yōu)點(diǎn)。首先,函數(shù)指針只能指向靜態(tài)
函數(shù),而delegate既可以引用靜態(tài)函數(shù),又可以引用非靜態(tài)成員函數(shù)。在引用非靜態(tài)成員函數(shù)時(shí),delegate不但保存了對
此函數(shù)入口指針的引用,而且還保存了調(diào)用此函數(shù)的類實(shí)例的引用。其次,與函數(shù)指針相比,delegate是面向?qū)ο蟆㈩愋?/p>
安全、可靠的受控(managed)對象。也就是說,runtime能夠保證delegate指向一個(gè)有效的方法,你無須擔(dān)心delegate會
指向無效地址或者越界地址。
2、創(chuàng)建步驟
(1)、聲明一個(gè)delegate對象,它應(yīng)當(dāng)與你想要傳遞的方法具有相同的參數(shù)和返回值類型。
(2)、創(chuàng)建delegate對象,并"將你想要傳遞的函數(shù)作為參數(shù)傳入"。
(3)、在要實(shí)現(xiàn)異步調(diào)用的地方,通過上一步創(chuàng)建的對象來調(diào)用方法。
3、代碼實(shí)現(xiàn)
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?DelegateEventApp
{
????class?DelegateUtilClass
????{
????????static?void?Main(string[]?args)
????????{
????????????Console.WriteLine("輸出結(jié)果是:");
????????????DelegateUtil?du?=?new?DelegateUtil(DelegateUtilClass.OutPut);?//?步驟2,創(chuàng)建DelegateUtil對象
????????????du("I'm?learning?delegate?now?!");//?步驟3,調(diào)用DelegateUtil
????????????Console.ReadLine();
????????}
????????public?delegate?void?DelegateUtil(string?sName);?//?步驟1,聲明delegate對象z
????????///?<summary>
????????///?描述:這是想要傳遞的方法,該方法與定義的委托(即DelegateUtil)具有相同的參數(shù)和返回值類型,函數(shù)名是不一樣的哦!
????????///?</summary>
????????///?<param?name="sName"></param>
????????public?static?void?OutPut(string?sName)
????????{
????????????Console.WriteLine("Hi,?"?+?sName);
????????}
????}
}
?
4、輸出結(jié)果
?
二、事件(Event)
1、事件定義
C#中的事件處理實(shí)際上是一種具有特殊簽名的delegate,象下面這個(gè)樣子:public delegate void MyEventHandler
(object sender, MyEventArgs e);
其中的兩個(gè)參數(shù),sender代表事件發(fā)送者,e是事件參數(shù)類。MyEventArgs類用來包含與事件相關(guān)的數(shù)據(jù),所有的事件參數(shù)
類都必須從System.EventArgs類派生。當(dāng)然,如果你的事件不含參數(shù),那么可以直接用System.EventArgs類作為參數(shù)。
2、創(chuàng)建步驟
(1)、定義delegate對象類型,它有兩個(gè)參數(shù),第一個(gè)參數(shù)是事件發(fā)送者對象,第二個(gè)參數(shù)是事件參數(shù)類對象。
(2)、定義事件參數(shù)類,此類應(yīng)當(dāng)從System.EventArgs類派生。如果事件不帶參數(shù),這一步可以省略。
(3)、定義"事件處理方法,它應(yīng)當(dāng)與delegate對象具有相同的參數(shù)和返回值類型"。
(4)、用event關(guān)鍵字定義事件對象,它同時(shí)也是一個(gè)delegate對象。
(5)、用+=操作符添加事件到事件隊(duì)列中(-=操作符能夠?qū)⑹录年?duì)列中刪除)。
(6)、在需要觸發(fā)事件的地方用調(diào)用delegate的方式寫事件觸發(fā)方法。一般來說,此方法應(yīng)為protected訪問限制,既
不能以public方式調(diào)用,但可以被子類繼承。名字是OnEventName。
(7)、在適當(dāng)?shù)牡胤秸{(diào)用事件觸發(fā)方法觸發(fā)事件。
3、代碼實(shí)現(xiàn)
?
?1?using?System;?2?using?System.Collections.Generic;
?3?using?System.Linq;
?4?using?System.Text;
?5?
?6?namespace?DelegateEventApp
?7?{
?8?????class?EventUtilClass
?9?????{
10?????????public?delegate?void?EventUtilEventHandler(object?sender,?System.EventArgs?e);//?步驟1?定義delegate對象
11?
12?????????//?步驟2(定義事件參數(shù)類)省略
13?
14?????????public?class?EventClass
15?????????{
16?????????????public?void?EventUtil_Function(object?sender,?System.EventArgs?e)
17?????????????{
18?????????????????Console.WriteLine("Hi,?This?is?using?event?to?print?!");??//?步驟3,定義事件處理方法,它與delegate對象具有相同的參數(shù)和返回值類型
19?????????????}
20?????????}
21?
22?????????//?步驟4,用event關(guān)鍵字定義事件對象
23?????????private?event?EventUtilEventHandler?utilEvent;
24?????????private?EventClass?eventClass;
25?
26?????????//構(gòu)造函數(shù)
27?????????public?EventUtilClass()
28?????????{
29?????????????eventClass?=?new?EventClass();
30?????????????this.utilEvent?+=?new?EventUtilEventHandler(eventClass.EventUtil_Function);?////?步驟5,用+=操作符將事件添加到隊(duì)列中
31?????????}
32?
33?????????//?步驟6,以調(diào)用delegate的方式寫事件觸發(fā)函數(shù)
34?????????protected?void?OnUtilEvent(System.EventArgs?e)
35?????????{
36?????????????if?(utilEvent?!=?null)
37?????????????{
38?????????????????utilEvent(this,?e);
39?????????????}
40?????????}
41?
42?????????public?void?RaiseEvent()
43?????????{
44?????????????EventArgs?e?=?new?EventArgs();
45?????????????this.OnUtilEvent(e);?//?步驟7,觸發(fā)事件
46?????????}
47?
48?????????static?void?Main(string[]?args)
49?????????{
50?????????????Console.WriteLine("輸出結(jié)果是:");
51?
52?????????????EventUtilClass?eventUtilClass?=?new?EventUtilClass();
53?????????????Console.WriteLine("請輸入一個(gè)字母\"A\"");
54?????????????string?sRead?=?Console.ReadLine();
55?????????????if?(sRead?==?"A")
56?????????????{
57?????????????????eventUtilClass.RaiseEvent();
58?????????????}
59?????????????else
60?????????????{
61?????????????????Console.WriteLine("輸入有錯(cuò)誤,未觸發(fā)事件!");
62?????????????}
63?????????????Console.ReadLine();
64?????????}
65?????}
66?}
67?
?
4、輸出結(jié)果
總結(jié)
- 上一篇: Spark提交 指定 kerberos
- 下一篇: ElasticSearch 新增节点,横