绑定不匹配错误:类型不是有效的替代
任务:我想通过PlayList类在“ImmutableSet”中存储“Song类对象”。
我有三节课。
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
答案 0 :(得分:5)
如果要添加Song
到ImmutableBinaryTree
首歌必须满足ImmutableBinaryTree
上类型参数的范围,这意味着它必须实现Comparable
接口,因为类型参数指定边界T extends Comparable<T>
。
public class Song implements Comparable<Song>{
@Override
public int compareTo(Song arg0) {
//provide implementation here
//See effective java for appropriate implementation conditions
return 0;
}
}