将事件侦听器添加到没有本机方法的事物的正确方法

发布于 2024-10-30 20:20:39 字数 189 浏览 6 评论 0原文

我有一个自定义类,当前未扩展任何内容(它用于在数据库上执行特定类型的查询),但我需要它向其父类发送事件。不过,addEventListener 没有在其中定义。

我注意到很多主要的 Flash 类都扩展了 EventDispatcher,那么如果没有其他选择,我应该这样做吗?只是有任何必须与其他类通信的类扩展 EventDispatcher 吗?

I have a custom class that is currently not extending anything (it's for executing specific types of queries on a database) but I need it to send an event to its parent class. addEventListener is not defined in it, though.

I notice a lot of the main flash classes extend EventDispatcher, so is that what I should do if there's no other alternative? Just have any class that will have to communicate with other classes extend EventDispatcher?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2024-11-06 20:20:39

您可以做一些事情,您绝对不限于使用事件。事件很好,因为它解耦了代码,并允许许多对象对单个事件进行操作,而无需调度事件的对象关心谁在监听或他们在做什么。如果您想分派事件,请扩展 EventDispatcher,或者将其作为对象的属性。 (有时,如果您已经扩展了另一个类,则可能需要将其定义为属性。)

或者,您的对象可以维护对另一个对象的引用,并显式调用它的方法。在这种情况下,您不需要扩展 EventDispatcher,但必须存储对要与之通信的所有其他对象的引用。

两种解决方案都实现相同的最终结果,这取决于情况和/或偏好。

我希望这有帮助。

There are a couple of things you can do, you are definitely not limited to using events. Events are nice because it decouples code, and allows many objects to act on a single event, without the object that dispatches the event from caring who is listening, or what they are doing. If you want to dispatch events, extend EventDispatcher, or have one as a property of your object. (Some times you may need to define it as a property if you are already extending another class.)

Alternatively your object can maintain a reference to another object, and explicitly call methods on it. In this case you do not need to extend EventDispatcher, but you must store a reference to all other objects you want to communicate with.

Both solutions accomplish the same end result, it comes down to situation and/or preference.

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文