1

鉴于:

public interface PrimaryKey<Key extends Comparable> {
    Key getKey();
}

public class PrimaryKeyComparator implements Comparator<PrimaryKey> {
    public int compare(PrimaryKey first, PrimaryKey second) {
        return first.getKey().compareTo(second.getKey());
    }
}

这种组合有效,但会发出有关原始类型的警告。我尝试了各种添加类型参数的方法,但我尝试的每种组合都会破坏代码。

4

1 回答 1

3

试试这个:

public interface PrimaryKey<TKey extends Comparable<TKey>> {
    TKey getId();
}

public class PrimaryKeyComparator<TKey extends Comparable<TKey>> 
                                 implements Comparator<PrimaryKey<TKey>> {
    public int compare(PrimaryKey<TKey> first, PrimaryKey<TKey> second) {
        return first.getId().compareTo(second.getId());
    }
}
于 2010-10-29T17:28:36.243 回答