Java 绑定不匹配错误:类型不是有效的替换项
绑定不匹配错误:类型不是有效的替代项 任务:我想通过PlayList类将“Song class object”存储在ImmutableSet中 我有三节课Java 绑定不匹配错误:类型不是有效的替换项,java,generics,mismatch,Java,Generics,Mismatch,绑定不匹配错误:类型不是有效的替代项 任务:我想通过PlayList类将“Song class object”存储在ImmutableSet中 我有三节课 public class ImmutableBinaryTree< T extends Comparable<T>> implements ImmutableSet<T> { } public class Song { } public class PlayList<T extends Comp
public class ImmutableBinaryTree< T extends Comparable<T>> implements ImmutableSet<T> { }
public class Song { }
public class PlayList<T extends Comparable<T>> {
private Song songs;
ImmutableSet<Song> immutableSet; // Bound Mismatch error occurring here
公共类ImmutableBinaryTree实现ImmutableSet{}
公共类歌曲{}
公共类播放列表{
私人歌曲;
ImmutableSet ImmutableSet;//此处发生绑定不匹配错误
如果要向ImmutableBinaryTree
添加歌曲,歌曲必须满足ImmutableBinaryTree
上类型参数的边界,这意味着它必须实现Comparable
接口,因为类型参数指定了T Extendes Comparable
的边界
公共类歌曲{
@凌驾
公共整数比较(歌曲arg0){
//在这里提供实现
//有关适当的实现条件,请参见有效的java
返回0;
}
}
这就是您的全部代码吗?似乎有什么东西丢失了。您好。代码很长,我刚刚提到了类。因为错误在泛型范围内。ImmutableSet是由ImmutableBinaryTree类实现的接口。公共接口ImmutableSet>{是否尝试在ImmutableBinaryTree
中存储歌曲
?是的,但需要将add(T)方法更改为add(Song)方法。我不想这样做。我想保留add(T)ImmutabaleBaryTree中的泛型class@user3094713我需要查看整个ImmutableBinaryTree
和PlayList
类。@user3094713太棒了!很高兴我能提供帮助!
public class Song implements Comparable<Song>{
@Override
public int compareTo(Song arg0) {
//provide implementation here
//See effective java for appropriate implementation conditions
return 0;
}
}