2013-12-12 40 views
3

約束不匹配錯誤:該類型是不是一個有效的替代約束不匹配錯誤:該類型是不是一個有效的替代

任務:我想存儲「宋類對象」在ImmutableSet通過播放列表類。

我有三個類。

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

這就是你的所有代碼?這似乎是缺少的東西 –

+0

嗨。守則很長,我剛纔提到了這些課程。因爲錯誤是在通用邊界中的某個地方。 ImmutableSet是由ImmutableBinaryTree類實現的接口。公共接口ImmutableSet > { – user3094713

+0

您是否試圖在'ImmutableBinaryTree'中存儲'Song'? –

回答

3

如果要添加一個SongImmutableBinaryTree歌曲必須滿足上ImmutableBinaryTree類型參數的邊界,這意味着它的,因爲類型參數指定的範圍T extends Comparable<T>必須實現Comparable接口。

public class Song implements Comparable<Song>{ 

    @Override 
    public int compareTo(Song arg0) { 
     //provide implementation here 
     //See effective java for appropriate implementation conditions 
     return 0; 
    } 

} 
+0

我也試過這個,但沒有運氣。 – user3094713

+0

@ user3094713我需要看到整個'ImmutableBinaryTree'和'PlayList'類。 –

+0

謝謝。它正在工作。 – user3094713

相關問題