2

我正在尝试编写 MVP 模式的接口和默认实现。但是,我收到一条关于我认为应该可行的错误消息。我收到的错误消息是:

required: CAP#1
  found: MVPPresenter<M,V>
  reason: actual argument MVPPresenter<M,V> cannot be converted to CAP#1 by method invocation conversion
  where V,M,P are type-variables:
    V extends IMVPView< ? extends IMVPPresenter< ?,V>> declared in class MVPPresenter
    M extends Object declared in class MVPPresenter
    P extends IMVPPresenter< ?,? extends IMVPView< P>> declared in interface IMVPView
  where CAP#1 is a fresh type-variable:
    CAP#1 extends IMVPPresenter< ?,V> from capture of ? extends IMVPPresenter< ?,V>

这对我来说没有意义,因为 MVPPresenter 应该是 CAP#1。有人可以解释为什么我不能这样做或提供解决问题的方法吗?

/**
 * Interface for the presenter in MVP
 *
 * @param <M> Model type
 * @param <V> View type
 */
public interface IMVPPresenter<M, V extends IMVPView<? extends IMVPPresenter<?, V>>> {
    ...
}

/**
 * Interface for the view in MVP
 *
 * @param <P> Presenter type
 */
public interface IMVPView<P extends IMVPPresenter<?, ? extends IMVPView<P>>> {
    ...
    public void setPresenter(P presenter);
}

/**
 * Default implementation of the presenter interface
 *
 * @param <M> Model Type
 * @param <V> View Type
 */
public class MVPPresenter<M, V extends IMVPView<? extends IMVPPresenter<?, V>>>
    implements IMVPPresenter<M, V> {
    ...
    protected void setView(V view) {
        ...
        view.setPresenter(this); // Error on this line
    }
}
4

1 回答 1

1

具体的课程必须……更具体一点。

public class MVPPresenter<M, V extends IMVPView<MVPPresenter<M,V>>>
  implements IMVPPresenter<M, V> {

  protected void setView(V view) {
    view.setPresenter(this); // No more error on this line
  }
}
于 2013-09-19T18:00:47.463 回答