Method参数如何与具有不同实现函数名称的功能接口兼容? [英] How is a Method reference compatible with functional interface with different implementing function names?
问题描述
这里,对静态方法isPrime()的引用作为第一个参数传递给numTest()。
这是因为isPrime与IntPredicate功能接口兼容。因此,
表达式MyIntPredicates :: isPrime求值为对象的引用,其中
isPrime()在IntPredicate中提供test()的实现。
Here, a reference to the static method isPrime( ) is passed as the first argument to numTest( ). This works because isPrime is compatible with the IntPredicate functional interface. Thus, the expression MyIntPredicates::isPrime evaluates to a reference to an object in which isPrime( ) provides the implementation of test( ) in IntPredicate.
isPrime()如何使用不同于test()的不同引用/名称来提供test()的实现。我想这就是在Java 8中实现它可以提供更多的灵活性和可能性。有人可以解释这是否是新的?它是如何运作的?
How can/does isPrime() provide an implementation of test() using a different reference/name other than test(). I guess this is the point of doing it in Java 8 gives more flexibility and possibilities. Can someone explain if this is new ? and how does it work this way ?
谢谢!
//Demonstrate a method reference for a static method.
//A functional interface for numeric predicates that operate
//on integer values.
interface IntPredicate {
//the abstact to be implemented with something compatible
boolean test(int n);
}
// This class defines three static methods that check an integer
// against some condition.
class MyIntPredicates {
// A static method that returns true if a number is prime.
static boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i <= n / i; i++) {
if ((n % i) == 0)
return false;
}
return true;
}
// A static method that returns true if a number is even.
static boolean isEven(int n) {
return (n % 2) == 0;
}
// A static method that returns true if a number is positive.
static boolean isPositive(int n) {
return n > 0;
}
}
public class MethodRefDemo {
// This method has a functional interface as the type of its
// first parameter. Thus, it can be passed a reference to any
// instance of that interface, including one created by a
// method reference.
static boolean numTest(IntPredicate p, int v) {
return p.test(v);
}
public static void main(String args[]) {
boolean result;
// Here, a method reference to isPrime is passed to numTest().
result = numTest(MyIntPredicates::isPrime, 17);
if (result)
System.out.println("17 is prime.");
// Next, a method reference to isEven is used.
result = numTest(MyIntPredicates::isEven, 12);
if (result)
System.out.println("12 is even.");
// Now, a method reference to isPositive is passed.
result = numTest(MyIntPredicates::isPositive, 11);
if (result)
System.out.println("11 is positive.");
}
}
推荐答案
这是一个工作中的lambdas的例子。它是java 8中的新功能,它基本上允许运行时使用您定义的实现为您创建功能接口的实例。
This is an example of lambdas at work. It is new in java 8 and it basically allows the runtime to create an instance of the functional interface for you with the implementation you define.
Brian Goetz写了 doc 。
Brian Goetz has written a doc on how lambdas (and method references) work at compile and runtime.
这篇关于Method参数如何与具有不同实现函数名称的功能接口兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!