gpt4 book ai didi

java - MVP 泛型问题(Java)

转载 作者:行者123 更新时间:2023-11-30 11:28:22 25 4
gpt4 key购买 nike

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

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
}
}

最佳答案

具体类必须……更具体一点。

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
}
}

关于java - MVP 泛型问题(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com