我正在编写同时实现Iterator
和Iterable
的Java类,因此可以将其传递给采用两者之一的方法。
import java.util.Iterator;
public class Iterable<T> implements java.lang.Iterable, Iterator{
private Iterator<T> iterator;
public Iterable(java.lang.Iterable<? extends T> iterable){
this(iterable.iterator());
}
public Iterable(Iterator<? extends T> iterator){
this.iterator=iterator;
}
public Iterator<T> iterator(){
return iterator;
}
public boolean hasNext(){
return iterator.hasNext();
}
public T next(){
return iterator.next();
}
}
在编译时,出现以下错误:
Iterable.java:11: error: incompatible types: Iterator<CAP#1> cannot be converted to Iterator<T>
this.iterator=iterator;
^
where T is a type-variable:
T extends Object declared in class Iterable
where CAP#1 is a fresh type-variable:
CAP#1 extends T from capture of ? extends T
1 error
有人知道是什么问题吗?