Java 为什么方法引用不';你不能用这个吗?
我有一个这样的方法Java 为什么方法引用不';你不能用这个吗?,java,generics,lambda,type-inference,method-reference,Java,Generics,Lambda,Type Inference,Method Reference,我有一个这样的方法 static R applyOthers(有些,有些,函数){ 列出其他=。。。; 返回函数appply(其他); } 现在当我尝试这个的时候 与一些(一些->{ List others1=applyOthers(一些,v->v);//有效 List others2=applyOthers(一些,函数::identity);//不 }); 一个错误,我明白了 incompatible types: unexpected static method <T>ide
static R applyOthers(有些,有些,函数){
列出其他=。。。;
返回函数appply(其他);
}
现在当我尝试这个的时候
与一些(一些->{
List others1=applyOthers(一些,v->v);//有效
List others2=applyOthers(一些,函数::identity);//不
});
一个错误,我明白了
incompatible types: unexpected static method <T>identity() found in unbound lookup
where T is a type variable:
T extends Object declared in method <T>identity()
不兼容类型:在未绑定查找中发现意外的静态方法标识()
其中T是一个类型变量:
T扩展在方法标识()中声明的对象
为什么::标识不起作用?函数f1=v1->v1;
Function<Object, Object> f1 = v1 -> v1;
Supplier<Object> s1 = Function::identity;
Supplier<Object> s2 = () -> Function.identity();
供应商s1=功能::标识;
供应商s2=()->Function.identity();
看这段代码,我想你只是误用了这里的方法引用。当我们说SomeObject::method
时,这个方法引用应该与一些函数接口匹配。在上面的例子中,Function::identity
是一种供应商实例,而不是java.util.Function.Function
实例。我们不应该说Function::identity是类似于-
供应商就是这样,伙计!它必须是Function.identity()
而不是Function::identity
。谢谢