gpt4 book ai didi

java - 理解 Java 谓词

转载 作者:行者123 更新时间:2023-12-02 03:02:48 27 4
gpt4 key购买 nike

我在理解 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com