Java Generics,实现Iterable类型的接口<e> </e>

时间:2015-04-12 15:21:00

标签: java generics

我有两个“集合”:一个是类GenericSet,另一个是接口ISetclass GenericSet implements ISet。类签名如下:

interface ISet<E entends Iterable<E>> { .... }

这个想法 - 当然 - 接口定义了一些我希望看到的实现类GenericSet实现的方法,其签名如下(这里是我被困的地方):

class GenericSet<E extends Iterable<E> implements ISet<Iterable<<E>> { .... }

我收到警告:

  

绑定不匹配:Iterable类型不是有界&gt;的有效替代;参数&gt; ISet类型

GenericSet的一般概念是 - 在实现ISet时 - 它能够促进接收多种类型的对象,创建一组接收类型。

我错过了什么?我知道错误信息解释了它;但是,由于我是Generics的新手,我不明白什么是有界参数的“有效替代品”。

2 个答案:

答案 0 :(得分:2)

据我所知,问题是ISet extend Iterable喜欢

public interface ISet<E extends Iterable<E>> extends Iterable<E> {
}

这样它满足了Iterable的条件。

另外,我认为你的意思是

class GenericSet<E extends Iterable<E>> implements ISet<E> {

}

然后可能用于实现Iterable Set

答案 1 :(得分:0)

实际上,这与接受的答案略有不同;但是,根据实施细节,以下内容也有效:

public interface ISet<E extends Iterable<E>> { .... }

反对

class GenericSet<E extends Iterable<E>> implements ISet<E> { .... }

这里的最小差异在于界面的签名。

相关问题