如何解决这个错误?我只尝试了 this
、(Article) this
和 super.iterable()
,但似乎没有任何效果。只有 Article
类型才会添加到此队列中。 Java 1.7.0_03。
[javac] C:\cygwin\home\Chloe\Queue.java:26: error: incompatible types
[javac] for (Article a: (Iterable) this) {
[javac] ^
[javac] required: Article
[javac] found: Object
这是源代码。
public class Queue<T> extends ArrayDeque {
public int words() {
int words;
for (Article a: (Iterable) this) {
}
}
}
最佳答案
队列的迭代器将返回 T
Object
实例,而不是 Article
。你的循环应该像这样:
public int words() {
int words;
for (Object a: this) {
// do something
}
}
或者,如果您需要文章队列,请这样声明该类:
public class Queue extends ArrayDeque<Article> {
// this queue will store Article instances
}
关于java - 扩展 Collection 时如何在 'this' 上使用 Iterable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11980733/