swing – 为什么EventListenerList在fireFooXXX()中向后遍历?
发布时间:2020-12-15 02:54:18 所属栏目:Java 来源:网络整理
导读:我不理解这段代码的基本原理,取自 javax.swing.event.EventListenerList文档: protected void fireFooXXX() { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first,no
我不理解这段代码的基本原理,取自
javax.swing.event.EventListenerList文档:
protected void fireFooXXX() { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first,notifying // those that are interested in this event for (int i = listeners.length-2; i>=0; i-=2) { if (listeners[i]==FooListener.class) { // Lazily create the event: if (fooEvent == null) fooEvent = new FooEvent(this); ((FooListener)listeners[i+1]).fooXXX(fooEvent); } } } >为什么列表会向后移动? 事件解雇在javax.swing.tree.DefaultTreeModel等等中以这种方式实施,所以显然我只是没有得到什么. 解决方法
>可能性能考虑因素:向后迭代更快,因为与0进行比较是一个单一的机器代码指令 – 许多以前的C程序员已经根深蒂固,尽管它现在相当无关紧要.请注意,无论如何都无法保证听众的通知顺序. >查看类的其余部分 – 它还存储侦听器的类型以提供类型安全性.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |