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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20539966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:38:12  来源:igfitidea点击:

Bound Mismatch Error: The type is not a valid substitute

javagenericsmismatch

提问by user3094713

Bound Mismatch Error: The Type is not a valid substitute

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

Task: I want to store "Song class object" in ImmutableSet through PlayList class.

任务:我想通过 PlayList 类将“歌曲类对象”存储在 ImmutableSet 中。

I have three classes.

我有三个班级。

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

采纳答案by Kevin Bowersox

If you are adding a Songto ImmutableBinaryTreesong must satisfy the bounds of the type parameter on ImmutableBinaryTree, meaning it must implement the Comparableinterface since the type parameter specifies the bounds T extends Comparable<T>.

如果您要添加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;
    }

}