在此示例中(这是从Oracle Site):新到Java - 自定义事件处理
// 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);
}
}
}
这是什么
听众[I] == FooListener.class
比较呢?它引发了我一些,因为它似乎将一个类的实例与一个类的类型进行比较。我如果说像
听众[I] .getClass()== Foolistener.class
但它不...可有人开导我在这里将能够理解吗?提前致谢!
对,所以它比较类型(在数组中)类... make的意义。 因此,如果我在数组中使用奇数并使用侦听器[i] .getClass()== FooListener.class,它会执行相同的操作吗? – Rene 2010-10-18 22:28:03
@Rene:是的,会的。 – SLaks 2010-10-18 22:31:34
不一定。添加它们时,指定它们将被“计数”为的侦听器的类型。索引为1的对象的具体类型实际上可能是索引为0的类的子类。 – Affe 2010-10-18 22:34:45