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 に答える