实现和集合 [英] Implementations and Collections

查看:136
本文介绍了实现和集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样无效...

  public ArrayList< Edge> getEdges(){

return A;

// A是类型为Action的数组列表。动作实现边缘。

}

接口Edge包括:public ArrayList getEdges / p>

   
return B;

// B是一个'Action'。动作实现边缘。

}

接口Edge包括:public Edge getEdges / p>

谢谢,
Chet

解决方案

code> Edge 是 Action ArrayList< Action> 的子类型不是 ArrayList< Edge> 的子类型。



使用 ArrayList <



您可以查看本教程的4.通配符部分,虽然我建议只读一遍,因为它是非常有帮助的。


Why does this not work...

public ArrayList<Edge> getEdges() {

return A;

//A is an Arraylist of type 'Action'. Action implements Edge.

}

the interface Edge includes: public ArrayList getEdges();

even though this does.

public Edge getEdges() {

return B;

//B is an 'Action'. Action implements Edge.

}

the interface Edge includes: public Edge getEdges();

Thank You, Chet

解决方案

Because while Edge is a subtype of Action, ArrayList<Action> is not a subtype of ArrayList<Edge>.

Use ArrayList<? extends Edge> instead.

You could take a look at this tutorial's 4. Wildcard section, although I'd suggest to just read through it, because it is really helpful.

这篇关于实现和集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆