📜  在泛型类 java 代码示例上具有可比性

📅  最后修改于: 2022-03-11 14:52:37.799000             🧑  作者: Mango

代码示例1
public class DoubleKey, J extends Comparable>
        implements Comparable> {

    private K key1;
    private J key2;

    public DoubleKey(K key1, J key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    public K getFirstKey() {
        return this.key1;
    }

    public J getSecondKey() {
        return this.key2;
    }

    public int compareTo(DoubleKey that) {

        int cmp = this.getFirstKey().compareTo(that.getFirstKey());
        if (cmp == 0)
            cmp = this.getSecondKey().compareTo(that.getSecondKey());
        return cmp;
    }
}