编译警告:未选中对 XXX 作为原始类型成员的调用
2022-09-03 02:00:37
我收到编译器警告:
警告:[未选中] 未选中对 setView(V) 作为原始类型 AbstractPresenter 成员的调用
this.presenter.setView(this);
其中 V 是类型变量:
V 扩展了在抽象摘要类中声明的抽象视图
该类的代码如下:AbstractPresenter
public abstract class AbstractPresenter<V extends AbstractView, M>
implements Presenter<V, M> {
private M model;
private V view;
@Override
public final V getView() {
return this.view;
}
public final void setView(V view) {
if (view == null) {
throw new NullPointerException("view cannot be null.");
}
if (this.view != null) {
throw new IllegalStateException("View has already been set.");
}
this.view = view;
}
@Override
public final M getModel() {
return this.model;
}
protected final void setModel(M model) {
if (model == null) {
throw new NullPointerException("model cannot be null.");
}
this.model = model;
}
}
该方法在以下类中调用:setView
AbstractView
public abstract class AbstractView<P extends AbstractPresenter> extends
UserControl {
private final P presenter;
public AbstractView(P presenter) {
this.presenter = presenter;
this.initialisePresenter();
}
private void initialisePresenter() {
if (this.presenter == null){
throw new IllegalStateException();
}
this.presenter.setView(this); //This is the call that raises the warning
}
protected P getPresenter() {
return this.presenter;
}
}
我已经搜索了其他成员关于同一警告的问题,并试图使解决方案适应我的问题,但它不起作用。
我不明白为什么在类的声明中强制键入类型时会引发警告:V
AbstractPresenter
public abstract class AbstractPresenter<V extends AbstractView, M>
implements Presenter<V, M>
这只是一个警告,我可以忽略它,但我想了解为什么会发生这种情况,我想让我的代码尽可能干净。