2015-06-13 16 views
14

我正在编译器警告的构件:编译警告:未经检查调用XXX作为原料类型

警告:作为原料类型AbstractPresenter

的成员[未选中]未经检查调用的setView(V)
this.presenter.setView(this); 

其中V是一个类型的变量:

V延伸AbstractView类声明AbstractPresenter

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> 

这只是一个警告,我可以忽略它,但我想理解它为什么会发生,并且希望尽可能清洁我的代码。

+0

代码中的循环引用让我很头疼。为什么观点需要有主持人,为什么主持人需要有观点?你不能把它转换成单向关系吗? – CKing

+1

http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – kervin

+0

@Chetan Kniger和Kervin谢谢你们,现在我知道什么是原始类型,实际上我有一个循环引用,如果我在AbstractPresenter类中声明V的声明,它将转换为循环继承...因此,我需要回到设计部分。我认为视图不需要在这里引用Presenter。 –

回答

9

你的类型是原始的 - 也就是说,你的泛型类型被绑定到一个类型本身有一个类型,但你没有提供,所以它是原始的。

更改要输入的类型边界。试试这个:

public abstract class AbstractPresenter<V extends AbstractView<V>, M> implements Presenter<V, M> 

public abstract class AbstractView<P extends AbstractPresenter<P> extends UserControl 
+3

我不能检查几个答案,所以我检查了一个,但所有的贡献帮助我解决了我的问题,所以在此线程中感谢所有。 –

6

你的问题是这一行:

public abstract class AbstractView<P extends AbstractPresenter> extends 

P被声明为延伸的原料AbstractPresenter一个类型。基本上,我们不知道该类型的VM是什么。

因此this.presenter是这种原始类型,我们不知道它的VM。因此,当您使用this调用setView时,编译器无法判断类型是否正确。

同样是真实的

public abstract class AbstractPresenter<V extends AbstractView, M> 

V是延伸的原料AbstractView一个类型,我们不知道它的基本类型是什么。因此,编译器无法完成泛型意义上的工作。

无论何时进行此类型声明,请记住在声明中指定所有泛型类型,并使用正确表示它们之间关系的类型变量。

2

我本来想添加一条评论,但没有,因为我没有足够的声望。

您的类型是生的。 AbstractView中的演示者是原始类型,因为传递给抽象视图的泛型参数是原始的

相关问题