public interface MyPredicate<T> {
public boolean test(Predicate<T> predicate, String s);
}
public class TestPredicate implements MyPredicate<String> {
@Override
public boolean test(Predicate<String> predicate, String s) {
return predicate.and(predicate).test(s);
}
public static void main(String[] args) {
TestPredicate testPredicate = new TestPredicate();
String str= "a";
boolean b = testPredicate.test((String s) -> {
if(str.length() >2) {
return true;
} else {
return false;
}
}, str);
System.out.println(b);
}
}
主要讲一下 and 方法
predicate.and(predicate) 能被 test() 方法调用说明 他是一个Predicate类型
进入 and方法
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
这里 (t) ->test(t) && other.test(t)是一个整体代表一个Predicate对象
而 test(t) && other.test(t)是一个整体也就是Predicate实现类的test()方法具体实现
test(t)是第一个Predicate对象返回值
other.test(t)第二个Predicate对象返回值
public boolean test(Predicate<String> predicate, String s) {
return predicate.and(predicate).test(s);
}
test方法实际调用的是 (t) -> test(t) && other.test(t)这个整体