我介绍了 java 8 的新特性。
我有以下课程:
class InstanceMethodClass {
Integer testStr(String str1,String str2) {
return str1.compareTo(str2);
}
}
和以下调用:
Arrays.sort(new String[]{"1", "3","2"},InstanceMethodClass::testStr );
之后我看到以下错误:
java: no suitable method found for sort(java.lang.String[],InstanceMe[...]stStr)
method java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; invalid method reference
cannot find symbol
symbol: method testStr(T,T)
location: class lambdas.staticReferences.InstanceMethodClass))
method java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
请解释我做错了什么?