- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我不明白为什么 method2 不编译而 method1 编译。我将 Eclipse 与 JavaSE 1.7 一起使用,在方法 2 上出现以下错误:
Multiple markers at this line
The type Enum<T> is not an interface; it cannot be specified as a bounded parameter
Bound mismatch: The type T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
public class Test {
public interface SomeInterface {
}
public static <T extends Enum<T> & SomeInterface> T method1() {
return null;
}
public static <T extends SomeInterface & Enum<T>> T method2() {
return null;
}
}
最佳答案
如果您查看 JLS 8.1.2 中类型参数边界的语法你会看到:
TypeBound:
extends TypeVariable
extends ClassOrInterfaceType {AdditionalBound}
AdditionalBound:
& InterfaceType
换句话说,只有第一个指定的类型可以是一个类——所有其余的都必须是接口(interface)。
除此之外,这可以防止指定多个类。
它也反射(reflect)了声明类时的方式,您必须首先放置它正在扩展的类,然后是它实现的接口(interface) - 而不是相反。
关于java - 为什么 <T extends Enum<T> & SomeInterface> 编译,而不是 <T extends SomeInterface & Enum<T>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194568/
我脑抽筋了:我有一个 public interface SomeInterface和一个 static private class SomeClass并试图返回 List从我的一种方法,但我得到了错误
我有一个接口(interface)和一个枚举如下: public interface SomeInterface { ...... } public enum SomeEnum impleme
我不明白为什么 method2 不编译而 method1 编译。我将 Eclipse 与 JavaSE 1.7 一起使用,在方法 2 上出现以下错误: Multiple markers at this
看似简单的问题,但我开始认为我正在以错误的方式实现某些目标。假设我有一个 Method对象已正确初始化。 我需要检查该方法是否会返回实现 Comparable 的对象界面。 问题是method.get
我有 3 个类代表 3 个不同的事物: Article , Photos , 和 Videos 所有 3 种事物都有 2 个共同属性:title和 image . 我有一个 interface称为 I
我想创建一个带有两个参数的类。一个应该简单地输入为 T。另一个应该输入为同时扩展 T 和 SomeInterface 的内容。 .当我尝试使用 public class SomeClass & T>
我得到 cannot use map[string]MyType literal (type map[string]MyType) as type map[string]IterableWithID
虽然涉及接口(interface)Comparable的定义 class B > 是众所周知且可实例化的,在扩展类而不是接口(interface)时,相同的用法不会以相同的方式工作。这是一个例子 cl
我是一名优秀的程序员,十分优秀!