Java或C#中的事件/代表 [英] Events/Delegates In Java or C#

查看:105
本文介绍了Java或C#中的事件/代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图了解事件/代表,但对两者之间的关系感到困惑。我知道代表允许你调用不同的函数,而不需要知道调用什么特定的函数。 (例如:图形函数需要接受不同功能的输入,以便绘制图形)。

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).

但我看不到在Events中使用委托。

But I don't see how delegates are used in Events.

有人可以构建一个简单的例子在伪代码或C#或Java中)说明了与事件有关的代表的工作原理?

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.)

我有一个关于事件和代表之间的差异的文章。这涵盖了更多细节下面提到的所有内容。

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 - 意思是添加此事件处理程序和删除此事件处理程序。在核心,这就是一个事件。

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#还有一个类似于场景的事件,这是一个快捷方式:

C# also has field-like events which are a shortcut:

 public event EventHandler Foo;

声明一个事件的字段,几乎是微不足道的添加/删除实现。在课堂内,参考 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.

基本思想是一个事件允许其他代码通过传递代理(事件处理程序)来订阅并取消订阅。通常,通过创建一个新的多播委托来实现订阅,该代理包含事件处理程序的以前的列表和新的。因此,如果您将事件处理程序存储在名为 myEventHandlers 的字段中,则订阅实现可能是:

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;

同样的取消订阅通常涉及创建一个新的多播委托而不使用指定的处理程序:

Similarly unsubscription usually involves creating a new multicast delegate without the specified handler:

myEventHandlers -= value;

然后,当你想提高/触发事件时,你只需要调用该多播委托 - 通常使用无效检查,以避免在没有人订阅的情况下抛出异常:

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);
}

使用事件,订阅者不了解对方,可以'自己提高事件(通常)。换句话说,它是一种封装模式,已被赋予语言和平台的状态。

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.

这篇关于Java或C#中的事件/代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆