gpt4 book ai didi

java - Java 新手 - 自定义事件处理程序

转载 作者:行者123 更新时间:2023-11-30 11:57:13 24 4
gpt4 key购买 nike

在此示例中(来自 Oracle Site):

// Notify all listeners that have registered interest for
// notification on this event type. The event instance
// is lazily created using the parameters passed into
// the fire method.

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

这是做什么的

listeners[i]==FooListener.class

比较吗?它让我有点失望,因为它似乎将类的实例与类的类型进行了比较。如果它说类似的话,我就能理解它

listeners[i].getClass() == Foolistener.class

但它没有...有人可以在这里启发我吗?提前致谢!

最佳答案

因为 getListenerList() 的文档就是这么说的。

public Object[] getListenerList()

Passes back the event listener list asan array of ListenerType-listenerpairs. Note that for performancereasons, this implementation passesback the actual data structure inwhich the listener data is storedinternally! This method is guaranteedto pass back a non-null array, so thatno null-checking is required in firemethods. A zero-length array of Objectshould be returned if there arecurrently no listeners. WARNING!!!Absolutely NO modification of the datacontained in this array should be made-- if any such manipulation is necessary, it should be done on a copyof the array returned rather than thearray itself.

数组是成对的Type和Instance。因此,索引 0 是在索引 1 处找到的实际监听器的类(或父类(super class)),索引 2 是在 3 处找到的实际监听器的类,依此类推。

关于java - Java 新手 - 自定义事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3963842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com