問題描述
我一直在嘗試了解事件/代表,但對兩者之間的關(guān)系感到困惑.我知道委托允許您調(diào)用不同的函數(shù),而無需知道正在調(diào)用的特定函數(shù).(例如:一個繪圖函數(shù)需要接受不同函數(shù)的輸入).
I've been trying to learn about events/delegates, but am confused about the relationship between the two. I know that delegates allow you to invoke different functions without needing to know what particular function is being invoked. (eg: a graphing function needs to accept inputs that are different functions to be graphed).
但我看不到事件中如何使用委托.
But I don't see how delegates are used in Events.
有人可以構(gòu)建一個簡單的示例(在偽代碼、C# 或 Java 中)來說明與事件相關(guān)的委托的工作原理嗎?
Can someone construct a simple example (in pseudocode or C# or Java) that illustrates the workings of Delegates as related to Events?
謝謝!
推薦答案
(這都是從 C# 的角度來看的.)
(This is all from a C# perspective.)
我有一篇關(guān)于事件和委托之間區(qū)別的文章.這更詳細(xì)地涵蓋了下面提到的所有內(nèi)容.
I have an article about the differences between events and delegates. That covers everything mentioned below in a lot more detail.
基本上,我喜歡將事件視為一個屬性——它是一對方法,僅此而已.一個事件不是 get/set,而是 add/remove——意思是添加這個事件處理程序"和刪除這個事件處理程序".從本質(zhì)上講,這就是一個事件.
Basically I like to think of an event as being like a property - it's a pair of methods, that's all. Instead of get/set, an event has add/remove - meaning "add this event handler" and "remove this event handler". At the core, that's all an event is.
C# 也有 field-like events 這是一個快捷方式:
C# also has field-like events which are a shortcut:
public event EventHandler Foo;
聲明一個字段和一個事件,幾乎是微不足道的添加/刪除實現(xiàn).在類中,引用 Foo
是指字段.在類之外,引用 Foo
是指事件.
declares both a field and an event, with a nearly trivial add/remove implementation. Within the class, referring to Foo
refers to the field. Outside the class, referring to Foo
refers to the event.
基本思想是事件允許其他代碼訂閱和取消訂閱,方法是傳入一個委托(事件處理程序).通常,訂閱是通過創(chuàng)建一個新的多播委托來實現(xiàn)的,該委托包含上一個事件處理程序列表和新的.因此,如果您將事件處理程序存儲在名為 myEventHandlers
的字段中,訂閱實現(xiàn)可能是:
The basic idea is that an event allows other code to subscribe to and unsubscribe from it, by passing in a delegate (the event handler). Usually, subscription is implemented by creating a new multicast delegate containing the previous list of event handlers and the new one. So if you're storing the event handlers in a field called myEventHandlers
, the subscription implementation might be:
myEventHandlers += value;
類似地取消訂閱通常涉及創(chuàng)建一個新的多播委托沒有指定的處理程序:
Similarly unsubscription usually involves creating a new multicast delegate without the specified handler:
myEventHandlers -= value;
然后,當(dāng)您想要引發(fā)/觸發(fā)事件時,您只需調(diào)用該多播委托 - 通常使用無效檢查以避免在沒有人訂閱時引發(fā)異常:
Then when you want to raise/fire the event, you just call that multicast delegate - usually with a nullity check to avoid an exception being thrown if no-one has subscribed:
EventHandler handler = myEventHandlers;
if (handler != null)
{
// You could pass in a different "sender" and "args" of course
handler(this, EventArgs.Empty);
}
使用事件,訂閱者彼此不了解,并且不能自己引發(fā)事件(通常).換句話說,它是一種封裝模式,在語言和平臺中都被賦予了地位.
Using events, the subscribers don't know about each other, and can't raise the event themselves (usually). In other words, it's a pattern of encapsulation, which has been given status within both the language and the platform.
這篇關(guān)于Java 或 C# 中的事件/委托的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!