我该如何解决这个错误?我只尝试了this(Article) thissuper.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

}

08-08 02:09
查看更多