实现和集合

发布于 2024-11-30 06:11:30 字数 431 浏览 0 评论 0原文

为什么这不起作用...

public ArrayList<Edge> getEdges() {

return A;

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

}

Edge 接口包括: public ArrayList getEdges();

尽管如此。

public Edge getEdges() {

return B;

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

}

Edge 接口包括: public Edge getEdges();

谢谢你, 切特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里梦着梦中梦 2024-12-07 06:11:30

因为虽然 EdgeAction 的子类型,但 ArrayList 不是 ArrayList 的子类型。

使用 ArrayList。

您可以查看本教程的 4. 通配符部分,尽管我建议您阅读它,因为它确实很有帮助。

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.

萌逼全场 2024-12-07 06:11:30

这是因为 ArrayList 不是协变 类型为 E。也就是说,您不能仅仅因为 Derived 继承自 Base 而用 ArrayList 实例替换 ArrayList代码>.

考虑这种情况:String 继承自 Object但是,如果这意味着您可以使用ArrayList作为ArrayList,那么以下代码是可能的

ArrayList<Object> list = new ArrayList<String>();
list.add(new Integer(5)); // Integer inherits from Object

:无法工作,因为您无法将 Integer 添加到 ArrayList。如果可以的话,那么这可能会发生:

ArrayList<String> stringList = (ArrayList<String>)list;
String string = stringList.get(0); // Not a string!

正如 Ziyao 指出的,正确的方法实现这个是用吗?扩展 Edge 语法。

This is because ArrayList<E> is not covariant on the type E. That is, you cannot substitute an instance of ArrayList<Derived> for ArrayList<Base> just because Derived inherits from Base.

Consider this case: String inherits from Object; however, if this meant you could use an ArrayList<String> as an ArrayList<Object> then the following code would be possible:

ArrayList<Object> list = new ArrayList<String>();
list.add(new Integer(5)); // Integer inherits from Object

The above can't work, because you can't add an Integer to an ArrayList<String>. If you could, then this could happen:

ArrayList<String> stringList = (ArrayList<String>)list;
String string = stringList.get(0); // Not a string!

As Ziyao has indicated, the correct way to implement this is to use the ? extends Edge syntax.

二货你真萌 2024-12-07 06:11:30

香蕉是一种水果。香蕉列表不是水果列表。

否则,有人可以构建一个香蕉列表,向您传递对水果列表的引用,然后您(正确地)在其中插入一个苹果。香蕉清单的所有者一定会感到惊讶。

A banana is a fruit. A list of bananas is not a list of fruit.

Oherwise someone could construct a list of bananas, pass you a reference to a list of fruit, and you'd (correctly) insert an apple in it. The owner of the list of the bananas would be rightfully surprised.

温柔少女心 2024-12-07 06:11:30

因为泛型类型必须始终相同,而不是从它扩展的类型,因此您可以像这样重写以使其正常工作:

public ArrayList<? extends Edge> getEdges() {

return A;

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

}

Because the generic type must always be the same, not something that extends from it you can rewrite like this for it to work:

public ArrayList<? extends Edge> getEdges() {

return A;

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文