[英]How can I use Java 11 repeat() method in a Method reference?
public class X{
public static void print(Integer n, Function<Integer, String> fn) {
System.out.println(fn.apply(n));
}
public static void main(String []args){
print(3, Integer::repeat()); //
}
}
Example :示例:
Since 3
is a given number, the value returned by the function and printed on the console should be "aaa"
.由于
3
是给定的数字,因此function返回并打印在控制台上的值应该是"aaa"
。
Your method reference is wrong in a couple ways.您的方法参考在几个方面是错误的。 First, a method reference doesn't end in parentheses
()
.首先,方法引用不以括号
()
结尾。 Second, the Integer
class doesn't have any repeat
method.其次,
Integer
class 没有任何repeat
方法。
The String
class defines the repeat
method , available for the first time in Java 11. But you want to execute on a specific instance of a string, namely the constant "a"
. String
class 定义了repeat
方法,在 Java 11 中首次可用。但是你想在字符串的特定实例上执行,即常量"a"
。
Try "a"::repeat
.试试
"a"::repeat
。
Method references as well as lambda expressions should conform to a function interface , by providing an implementation of the abstract method defined by this interface.方法引用以及lambda 表达式应符合function 接口,通过提供此接口定义的抽象方法的实现。
And interface Function
declares a method apply()
并且接口
Function
声明了一个方法apply()
R apply(T t);
You can implement it like that with lambda expression:您可以使用 lambda 表达式实现它:
Function<Integer, String> fun = num -> String.valueOf(num);
Method repeat()
is an instance method of the String
class, ie you have to invoke it on the instance of the String
.方法
repeat()
是String
class 的实例方法,即您必须在String
的实例上调用它。 So, need to define this string either by outside the lambda or hard-code it inside the body of lambda.因此,需要在 lambda 外部定义此字符串,或在 lambda 的主体内对其进行硬编码。
public static final String str = "a";
Function<Integer, String> fun = num -> str.repeat(num);
Now, in order to transform this lambda expression in a method reference , let's first pose the question of what the method reference is.现在,为了将这个lambda 表达式转化为方法引用,我们首先提出一个问题,方法引用是什么。
Method reference - is the way to utilize an existing method to provide an implantation of the behavior defined by a function interface .方法参考- 是利用现有方法提供由function 接口定义的行为植入的方法。
There are four kinds of method references (a quote from the Oracles's tutorial ):有四种方法引用(引用自Oracles 教程):
ContainingClass::staticMethodName
ContainingClass::staticMethodName
containingObject::instanceMethodName
containingObject::instanceMethodName
ContainingType::methodName
ContainingType::methodName
的任意 object 的实例方法ClassName::new
ClassName::new
Note that the syntax of method references doesn't require parentheses ()
because that how the language was designed, and I guess because method references are intended to be concise and expressive.请注意,方法引用的语法不需要括号
()
,因为语言是这样设计的,我猜是因为方法引用的目的是简洁和富有表现力。
The type of method reference that we need to implement this function using repeat()
method is a reference to an instance method of a particular object (because repeat()
is an instance method).我们需要使用
repeat()
方法实现这个 function 的方法引用类型是对特定 object 的实例方法的引用(因为repeat()
是一个实例方法)。 And that's how it might look like:这就是它的样子:
public static final String str = "a";
Function<Integer, String> fun = str::repeat;
print(3, fun); // or print(3, str::repeat);
Note that variables used inside both method references and lambda expressions must be final or effectively final, ie should be assigned only once.请注意,在方法引用和 lambda 表达式中使用的变量必须是最终的或实际上是最终的,即只能分配一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.