[英]MVP Generics Issue (Java)
I am trying to write a interfaces and default implementations of the MVP pattern. 我正在尝试编写MVP模式的接口和默认实现。 However, I am receiving an error message on something that I think should be possible.
但是,我收到一些我认为应该可行的错误消息。 The error message that I receive is:
我收到的错误消息是:
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>
This does not make sense to me because MVPPresenter should be a CAP#1. 这对我来说没有意义,因为MVPPresenter应该是CAP#1。 Can someone please explain why I can't do this or provide a way to fix the problem?
有人可以解释为什么我不能这样做或提供解决问题的方法吗?
/**
* 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
}
}
Concrete classes must be … well a bit more concrete. 具体的类必须……具体得多。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.