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

时间:2023-01-18 16:29:55

Bound Mismatch Error: The Type is not a valid substitute

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

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

任务:我想通过播放列表类在immutable et中存储“Song类对象”。

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

1 个解决方案

#1


3  

If you are adding a Song to ImmutableBinaryTree song must satisfy the bounds of the type parameter on ImmutableBinaryTree, meaning it must implement the Comparable interface since the type parameter specifies the bounds T extends Comparable<T>.

如果要将歌曲添加到ImmutableBinaryTree Song,则必须满足ImmutableBinaryTree上的类型参数的界限,这意味着它必须实现可比接口,因为类型参数指定了边界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;
    }

}

#1


3  

If you are adding a Song to ImmutableBinaryTree song must satisfy the bounds of the type parameter on ImmutableBinaryTree, meaning it must implement the Comparable interface since the type parameter specifies the bounds T extends Comparable<T>.

如果要将歌曲添加到ImmutableBinaryTree Song,则必须满足ImmutableBinaryTree上的类型参数的界限,这意味着它必须实现可比接口,因为类型参数指定了边界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;
    }

}