gpt4 book ai didi

Java 泛型,实现 Iterable 类型的接口(interface)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:48 26 4
gpt4 key购买 nike

我有两个“集合”:一个是类,GenericSet,另一个是接口(interface),ISet类 GenericSet 实现 ISet。类签名如下:

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

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

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

我收到警告:

Bound mismatch: The type Iterable is not a valid substitute for the bounded > parameter > of the type ISet

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

我错过了什么?我知道错误消息对此进行了解释;然而,由于我是泛型的新手,我不明白什么是有界参数的“有效替代品”。

最佳答案

据我所知,问题是ISet extend Iterable like

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

这样它就满足了也是 Iterable 的条件。

另外,我想你的意思是

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

}

然后大概用于实现 Iterable Set

关于Java 泛型,实现 Iterable<E> 类型的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com