我知道Java的泛型类型存在各种反直觉属性.这是我不理解的一个,我希望有人可以向我解释.为类或接口指定类型参数时,可以绑定它以使其必须实现多个接口public class Foo<T extends InterfaceA & InterfaceB>.但是,如果您要实例化实际对象,则此功能不再起作用.List<? extends InterfaceA>很好,但List<? extends InterfaceA & InterfaceB>无法编译.请考虑以下完整代码段:
import java.util.List;
public class Test {
static interface A {
public int getSomething();
}
static interface B {
public int getSomethingElse();
}
static class AandB implements A, B {
public int getSomething() { return 1; }
public int getSomethingElse() { return 2; }
}
// Notice the multiple bounds here. This works.
static class AandBList<T extends A & B> {
List<T> …Run Code Online (Sandbox Code Playgroud)