我想写一些功能工厂.它应该是一个函数,它被称为一次不同的策略作为参数.它应该返回一个函数,该函数根据参数选择其中一个策略,该参数由谓词实现.那么,更好condition3地了解更好的理解.问题是,它没有编译.我认为因为编译器无法弄清楚,功能接口H可以通过实现来实现.没有泛型它工作正常.
@FunctionalInterface
public interface Finder<T, S> {
Stream<S> findBest (T t);
// Valid code
static <P, Q> Finder<P, Q> condition1 () {
return p -> null;
}
// Valid code, but selects just one of the H's when the method is invoked
static <P, Q, H extends Finder<P, Q>> H condition2 (Pair<Predicate<P>, H>... hs) {
return hs[0].getRight ();
}
// Should return a method, which selects the appropiate H
// whenever it is invoked with an …Run Code Online (Sandbox Code Playgroud)