Java 8中的方法引用是否具有具体的类型,如果是,它是什么? [英] Does a method reference in Java 8 have a concrete type and if so, what is it?
问题描述
这个问题与另一个问题密切相关。但是,我觉得这个问题的接受答案并不完全确定。
所以,是中的方法引用的类型Java 8?这里有一个演示如何将一个方法引用转换(解除)到一个 java.util.function.Function
中:
package java8.lambda;
import java.util.function.Function;
public class Question {
public static final class Greeter {
private final String salutation;
public Greeter(final String salutation){
this.salutation = salutation;
}
public String makeGreetingFor(final String name){
return String.format(%s,%s!,salutation,name);
public static void main(String [] args){
final greeter helloGreeter = new Greeter(Hello);
identity(helloGreeter :: makeGreetingFor)
andandhen(g - ><<+ g +>>>)
。适用( 乔);
//编译错误:对象不是函数接口
//函数
// .identity()
// .apply(helloGreeter :: makeGreetingFor)
// .and Then(g - ><<+ g +>>>)
// .apply(Joe);
函数
。<函数< String,String>> identity()
.apply(helloGreeter :: makeGreetingFor)
.andThen(g - > <<+ g +>>>)
.apply(Joe); (< lambda表达式>)'
//(helloGreeter :: makeGreetingFor)
// .andThen(g - >
//编译错误:无法解析方法' ;<<<+ g +>>>)
// .apply(Joe);
// java.lang.invoke.LambdaMetafactory ???
}
private static< I,O>功能< I,O>身份(final Function fun1){
return fun1;
$ b 那么,向前)将方法引用转换为可以传递的编译/具体类型的方法?解析方法
方法引用只是一个使用传递参数作为输入参数的函数的语法糖。所以,你可以这样分配它们:
Runnable runnable = System.out :: println;
Consumer consumer = System.out :: println;
类型是根据上下文推断出来的。
您的情况:
函数< String,String> foo = helloGreeter :: makeGreetingFor;
等于:
函数< String,String> foo = s - > helloGreeter.makeGreetingFor(一个或多个);
This question is pretty closely related to another one. However, I feel like the accepted answer to that question is not quite as definitive.
So, what is the type of a method reference in Java 8? Here's a little demonstration of how a method reference can be "cast" (lifted?) into a java.util.function.Function
:
package java8.lambda;
import java.util.function.Function;
public class Question {
public static final class Greeter {
private final String salutation;
public Greeter(final String salutation) {
this.salutation = salutation;
}
public String makeGreetingFor(final String name) {
return String.format("%s, %s!", salutation, name);
}
}
public static void main(String[] args) {
final Greeter helloGreeter = new Greeter("Hello");
identity(helloGreeter::makeGreetingFor)
.andThen(g -> "<<<" + g + ">>>")
.apply("Joe");
//Compilation error: Object is not a function interface
// Function
// .identity()
// .apply(helloGreeter::makeGreetingFor)
// .andThen(g -> "<<<" + g + ">>>")
// .apply("Joe");
Function
.<Function<String,String>>identity()
.apply(helloGreeter::makeGreetingFor)
.andThen(g -> "<<<" + g + ">>>")
.apply("Joe");
//Compilation error: Cannot resolve method 'andThen(<lambda expression>)'
// (helloGreeter::makeGreetingFor)
// .andThen(g -> "<<<" + g + ">>>")
// .apply("Joe");
// java.lang.invoke.LambdaMetafactory ???
}
private static <I,O> Function<I,O> identity(final Function<I,O> fun1) {
return fun1;
}
}
So, is there a less painful (more straight-forward) way of casting a method reference into a compiled/concrete type which can be passed around?
Method references are just a syntactic sugar for a function that uses passed parameter as an input argument. So, you can assign them this way:
Runnable runnable = System.out::println;
Consumer consumer = System.out::println;
types are inferred and depend on a context.
Your case:
Function<String, String> foo = helloGreeter::makeGreetingFor;
and it is equal to:
Function<String, String> foo = s -> helloGreeter.makeGreetingFor(s);
这篇关于Java 8中的方法引用是否具有具体的类型,如果是,它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!