在下面的简单代码中,我可以看到“工厂”接口(interface),它们之间的唯一区别是 Factory0Bad
扩展了 Supplier
:
@FunctionalInterface
public static interface Factory0Bad<T> extends Supplier<T> {
T get();
}
@FunctionalInterface
public static interface Factory0Good<T> {
T get();
}
public static class Handler {}
public static class HandlerA extends Handler {}
public static void main(String[] args) {
// this line compiles
Factory0Good<? extends Handler> good = HandlerA::new;
// this does not!
Factory0Bad<? extends Handler> bad = HandlerA::new;
}
所以行
Factory0Good<? extends Handler> good = HandlerA::new;
被编译器(1.8.0u05)接受,但是这一行
Factory0Bad<? extends Handler> bad = HandlerA::new;
不是。
这是为什么?我在这里错过了什么?是 javac 中的错误吗?
最佳答案
// this does not!
// 1.8.0_05 - fails
// 1.8.0_40 - succeeds
// 1.8.0_45 - succeeds
Factory0Bad<? extends Handler> bad = HandlerA::new;
关于java - 扩展其他功能的接口(interface)不能 - 不能为它分配构造函数引用 - 它是编译器错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31053331/