每个对象都存在equals方法.其余的你必须自己创造.您可以创建一个具有这些方法的界面,以便所有孩子都拥有它们.
这应该工作:
public interface IComparable extends Comparable {
boolean isLessThan(T other);
boolean isLessOrEqualTo(T other);
boolean isGreaterOrEqualTo(T other);
boolean isGreaterThan(T other);
}
您可以为上述每种方法添加默认方法.像这样:
public interface IComparable extends Comparable {
default boolean isLessThan(T other) {
return compareTo(other) < 0;
}
default boolean isLessOrEqualTo(T other) {
return compareTo(other) <= 0;
}
default boolean isGreaterOrEqualTo(T other) {
return compareTo(other) >= 0;
}
default boolean isGreaterThan(T other) {
return compareTo(other) > 0;
}
}