绑定不匹配错误:类型不是有效替代

时间:2013-12-12 09:47:31

标签: java generics mismatch

绑定不匹配错误:类型不是有效的替代

任务:我想通过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

1 个答案:

答案 0 :(得分:5)

如果要添加SongImmutableBinaryTree首歌必须满足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;
    }

}