在此示例中(這是從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