OnProgressEventHandler

OnProgressEventHandler

EventHandler.java:

public abstract class EventHandler<E> {
    public abstract void HandleEvent(Object sender, E e);
}


Foo.java:

private OnProgressEventHandler m_OnProgressEventHandler = new OnProgressEventHandler();
private class OnProgressEventHandler extends EventHandler<ProgressEventArgs> {
    public void HandleEvent(Object sender, ProgressEventArgs args) {
        /*...*/
    }
}


ProgressEventArgs.java:

public class ProgressEventArgs extends EventArgs {
    /*...*/
}


Bar.java:

public void addProgressEventListener(EventHandler<EventArgs> listener) {
    m_OnProgress.addListener(listener);
}


这行:

myBarObject.addProgressEventListener(m_OnProgressEventHandler);


原因:


  方法中的方法addProgressEventListener(EventHandler)
  Bar类型不适用于参数(Foo.OnProgressEventHandler)


我不明白为什么它不能编译,因为OnProgressEventHandler扩展了EventHandler<ProgressEventArgs>。我确定这是我想念的琐事,但我不知道为什么。

最佳答案

更改

public void addProgressEventListener(EventHandler<EventArgs> listener) {
    m_OnProgress.addListener(listener);
}




public void addProgressEventListener(EventHandler<? extends EventArgs> listener) {
    m_OnProgress.addListener(listener);
}

10-04 14:32