2008-11-24 60 views
0

我有一大堆的通用接口和类为什么这个通用接口堆栈不起作用?

public interface IElement { 
// omited 
} 

class Element implements IElement { 
// omited 
} 

public interface IElementList<E extends IElement> extends Iterable { 
    public Iterator<E> iterator(); 
} 

class ElementList implements IElementList<Element> { 

    public Iterator<Element> iterator() { 
     // omited 
     } 
} 


public interface IElementListGroup<E extends IElementList<? extends IElement>> { 
    public E getChosenElementList(); 
} 


class ElementListGroup implements IElementListGroup<ElementList> { 
    public ElementList getChosenElementList() { 
     // omited 
    } 
} 

然后一个简单的代码

ElementListGroup group; 

for(Element e : group.getChosenElementList()) 
{ 
// omited 
} 

并与throwe“无法从元素类型的对象转换为元件”编译器错误行关键字。

在此先感谢。

回答

7

IElementList需要执行Iterable<E>。否则,接口指定Iterator iterator(),而不是Iterator<E> iterator()。这使编译器认为你正在迭代Objects。

我做了这个改变,它编译得很好(在添加了一些空返回值后)。

+0

我很惊讶,编译器并没有在使用原始类型咬住了OP的头。或者,OP可能有忽视警告的习惯,在这种情况下,他们应该得到他们所得到的。 :-P – 2008-11-24 16:19:30

0

你的函数返回一个元素列表不是一个元素,而元素列表不是一个迭代过元素