这段代码有什么问题?我需要创建一个依赖 BiFunction 的工厂方法来完成它的工作。
编译器告诉我:
The method apply(Class<T>, Supplier<I>)
in the type BiFunction<Class<T>,Supplier<I>,T>
is not applicable for the arguments (Class<A>, B::new)
但是 B 扩展了 A,B::new 为 B 生成了供应商 ...
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
public class BiFunctionTest {
static interface A {
}
static class B implements A {
}
static interface C {
}
static class D implements C {
}
private A a;
private C c;
public static <T, I extends T> BiFunctionTest create(
BiFunction<Class<T>, Supplier<I>, T> fn) {
BiFunctionTest o = new BiFunctionTest();
o.a = fn.apply(A.class, B::new);
o.c = fn.apply(C.class, D::new);
return o;
}
@Test
public void testBiFunction() {
BiFunctionTest o = BiFunctionTest.create((i, s) -> s.get());
assertEquals(B.class, o.a.getClass());
assertEquals(D.class, o.c.getClass());
}
}