以下简单程序使用Eclipse编译器编译,但不编译Javac:
public class Test {
public static interface Function<T1, T2, Boolean> {
Boolean apply(T1 t1, T2 t2);
}
private static <T extends Comparable<T>> Function<T, T, Boolean> _cmp(final boolean lt) {
return new Function<T, T, Boolean>() {
@Override
public Boolean apply(T t1, T t2) {
if (lt) {
return (t1.compareTo(t2) < 0);
} else {
return (t1.compareTo(t2) > 0);
}
}
};
}
public static Function LESS_THAN = _cmp(true); // error line
public static Function GREATER_THAN = _cmp(false); // error line
}
Javac的错误消息:
java: P:\java-tool\src\main\java\T3.java:20: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T
found : <T>T3.Function<T,T,java.lang.Boolean>
required: T3.Function
答案 0 :(得分:2)
类型推断不适用于版本1.6 -
尝试手动指定类型参数。例如。 -
public static Function LESS_THAN = Test.<Integer>_cmp(true); // error line
请注意,您正在使用自己的泛型类的原始版本。