事件侦听器和事件侦听器之间有什么区别?Java中的处理程序? [英] What's the difference between Event Listeners & Handlers in Java?

查看:33
本文介绍了事件侦听器和事件侦听器之间有什么区别?Java中的处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中一般来说,有监听器&事件处理程序.
我的意思是我在不知不觉中使用它们,只是 API 中可用的那个.

In general terms of java, there are listeners & handlers for events.
I mean I use them unknowingly, just whichever is available in the API.

我的问题是,在什么情况下我们使用侦听器,在什么情况下我们使用事件处理程序?

My question is, in what case do we use listeners and in what case do we use handlers for events?

它们之间有什么区别?特点??

What's the difference between them? Characteristics??

我搜索了原因,但找不到对 Java 的正确解释.

I've searched for reasons and I couldn't find a proper explanation for Java.

推荐答案

侦听器和处理程序之间没有正式定义的区别.有些人可能会争辩说它们是可以互换的.然而,对我来说,它们的含义略有不同.

There's no formally defined difference between listeners and handlers. Some people would probably argue that they are interchangeable. To me however, they have slightly different meaning.

监听器是一个从源订阅事件的对象.参见观察者模式.通常你可以有多个监听器订阅每种类型的事件,它们通过 addXyzListener 方法添加.

A listener is an object that subscribes for events from a source. Cf. the observer pattern. Usually you can have many listeners subscribing for each type of event, and they are added through addXyzListener methods.

示例: MouseListener 在 Java API 中.

Example: The MouseListener in the Java API.

处理程序是负责处理特定事件的对象.一个典型的场景是为特定的事件/任务提供一个处理程序作为构造函数的参数,或者通过一个 setXyzHandlerset 处理程序> 方法.换句话说,对于每种类型的事件,您通常有一个处理程序.

A handler is an object that is responsible for handling certain events. A typical scenario would be to provide a handler for a specific event/task as an argument to a constructor, or set the handler through a setXyzHandler method. In other words, you usually have one handler for each type of event.

示例: MemoryHandler 在 Java API 中.

Example: The MemoryHandler in the Java API.

这篇关于事件侦听器和事件侦听器之间有什么区别?Java中的处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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