我正在尝试编写一个需要两个Comparables 的泛型max函数.
到目前为止我有
public static <T extends Comparable<?>> T max(T a, T b) {
if (a == null) {
if (b == null) return a;
else return b;
}
if (b == null)
return a;
return a.compareTo(b) > 0 ? a : b;
}
Run Code Online (Sandbox Code Playgroud)
这无法编译
The method compareTo(capture#5-of ?) in the type Comparable<capture#5-of ?> is not applicable for the arguments (T)
Run Code Online (Sandbox Code Playgroud)
我认为这是说,?in Comparable<?>可以被解释为参数a的一种类型,而参数b的另一种类型,因此它们无法进行比较.
我如何从这个洞中挖掘自己?