Java-使用不带lambda表达式的谓词
我有以下要求:- Employee.javaJava-使用不带lambda表达式的谓词,java,java-8,predicate,method-reference,Java,Java 8,Predicate,Method Reference,我有以下要求:- Employee.java public boolean isAdult(Integer age) { if(age >= 18) { return true; } return false; } private Integer age; Predicate<Integer> isAdult; public PredicateAnotherClass(Integer age, Predicate<Int
public boolean isAdult(Integer age) {
if(age >= 18) {
return true;
}
return false;
}
private Integer age;
Predicate<Integer> isAdult;
public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) {
this.age = age;
System.out.println("Test result is "+ isAdult(age));
}
public void testPredicate() {
System.out.println("Calling the method defined in the manager class");
}
Predicate.java
public boolean isAdult(Integer age) {
if(age >= 18) {
return true;
}
return false;
}
private Integer age;
Predicate<Integer> isAdult;
public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) {
this.age = age;
System.out.println("Test result is "+ isAdult(age));
}
public void testPredicate() {
System.out.println("Calling the method defined in the manager class");
}
我在System.out.println(“测试结果是”+isAdult(age))中得到编译错误谓词
类中的code>
让我知道如何解决这个问题。如果我需要提供任何其他信息。谓词接口有方法test()
。应按以下方式使用此方法:
isAdult.test(age)
此方法对给定参数计算此谓词。如果输入参数与谓词匹配,则返回true,否则返回false,谓词具有在处理streams/optionals时使用的测试方法
public PredicateAnotherClass(Integer age, Predicate<Integer> isAdultFilter) {
this.age = age;
System.out.println("Test result is "+ isAdultFilter.test(age));
}
公共谓词OtherClass(整数年龄,谓词isAdultFilter){
这个。年龄=年龄;
System.out.println(“测试结果为”+IsadaltFilter.Test(年龄));
}
这看起来有点可疑,您关心员工是否是成年人,因此您的方法应该真正将员工
作为参数和谓词
,如下所示:
private static void testEmployee(Employee emp, Predicate<Employee> predicate) {
boolean result = predicate.test(emp);
System.out.println(result);
}
问题是,你也可以将此方法用于其他谓词,比如说你想测试性别或收入等。我自己刚刚找到了它,但仍然继续向上投票并接受你的答案,谢谢你的及时回复。这真的很混乱,不应该是谓词isAdult
吗。如果员工是成年人,你会在意的。那么,请结束问题。