我在理解 Java 谓词时遇到问题...
示例:
public class UserPredicates {
public static Predicate<User> isNameEmpty() {
return p -> p.getFirstName().isEmpty() && p.getLastName().isEmpty();
}
}
上面的例子让我很困惑,它怎么知道p
有方法getFirstName()
和getLastName()
?
如果我理解正确的话,isNameEmpty()
返回一个带有一个参数的函数 (p
),但是编译器真的通过查看返回类型来确定类型吗?
返回的函数是 Predicate 接口(interface)的 test()
?
最佳答案
Predicate<User>
表示采用 User
的函数作为其参数,并返回 boolean
.
参见Javadoc :
Interface Predicate
...
Type Parameters:
T - the type of the input to the predicate
关于java - 理解 Java 谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197972/