• <i id='peukq'><tr id='peukq'><dt id='peukq'><q id='peukq'><span id='peukq'><b id='peukq'><form id='peukq'><ins id='peukq'></ins><ul id='peukq'></ul><sub id='peukq'></sub></form><legend id='peukq'></legend><bdo id='peukq'><pre id='peukq'><center id='peukq'></center></pre></bdo></b><th id='peukq'></th></span></q></dt></tr></i><div id='peukq'><tfoot id='peukq'></tfoot><dl id='peukq'><fieldset id='peukq'></fieldset></dl></div>
  • <tfoot id='peukq'></tfoot>
      <bdo id='peukq'></bdo><ul id='peukq'></ul>
    <legend id='peukq'><style id='peukq'><dir id='peukq'><q id='peukq'></q></dir></style></legend>

        <small id='peukq'></small><noframes id='peukq'>

      1. Java 或 C# 中的事件/委托

        时间:2023-09-29

          • <bdo id='vLL1O'></bdo><ul id='vLL1O'></ul>

            • <legend id='vLL1O'><style id='vLL1O'><dir id='vLL1O'><q id='vLL1O'></q></dir></style></legend>
              1. <small id='vLL1O'></small><noframes id='vLL1O'>

                <tfoot id='vLL1O'></tfoot>
                <i id='vLL1O'><tr id='vLL1O'><dt id='vLL1O'><q id='vLL1O'><span id='vLL1O'><b id='vLL1O'><form id='vLL1O'><ins id='vLL1O'></ins><ul id='vLL1O'></ul><sub id='vLL1O'></sub></form><legend id='vLL1O'></legend><bdo id='vLL1O'><pre id='vLL1O'><center id='vLL1O'></center></pre></bdo></b><th id='vLL1O'></th></span></q></dt></tr></i><div id='vLL1O'><tfoot id='vLL1O'></tfoot><dl id='vLL1O'><fieldset id='vLL1O'></fieldset></dl></div>
                  <tbody id='vLL1O'></tbody>

                  本文介绍了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).

                  但我看不到事件中如何使用委托.

                  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# 也有 field-like events 这是一个快捷方式:

                  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# 中的事件/委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何设置 Java 默认按钮以对 ENTER 键 _released_ 做出 下一篇:无法从 JavaFX 中的 MenuItem 获取场景

                  相关文章

                  最新文章

                1. <tfoot id='oELxO'></tfoot>

                    <legend id='oELxO'><style id='oELxO'><dir id='oELxO'><q id='oELxO'></q></dir></style></legend>

                    <small id='oELxO'></small><noframes id='oELxO'>

                    • <bdo id='oELxO'></bdo><ul id='oELxO'></ul>

                    <i id='oELxO'><tr id='oELxO'><dt id='oELxO'><q id='oELxO'><span id='oELxO'><b id='oELxO'><form id='oELxO'><ins id='oELxO'></ins><ul id='oELxO'></ul><sub id='oELxO'></sub></form><legend id='oELxO'></legend><bdo id='oELxO'><pre id='oELxO'><center id='oELxO'></center></pre></bdo></b><th id='oELxO'></th></span></q></dt></tr></i><div id='oELxO'><tfoot id='oELxO'></tfoot><dl id='oELxO'><fieldset id='oELxO'></fieldset></dl></div>