泛型是棘手的.看起来它们在不同版本的Java中被区别对待.
此代码在Java 7中成功编译,无法使用Java 8进行编译.
import java.util.EnumSet;
public class Main {
public static void main(String[] args) {
Enum foo = null;
tryCompile(EnumSet.of(foo));
}
static <C extends Enum<C> & Another> void tryCompile(Iterable<C> i) {}
static interface Another {}
}
Run Code Online (Sandbox Code Playgroud)
这是来自Java 8的错误消息.我用这个来编译它:http://www.compilejava.net/
/tmp/java_A7GNRg/Main.java:6: error: method tryCompile in class Main cannot be applied to given types;
tryCompile(EnumSet.of(foo));
^
required: Iterable<C>
found: EnumSet
reason: inferred type does not conform to upper bound(s)
inferred: Enum
upper bound(s): Enum<Enum>,Another
where C is a type-variable: …Run Code Online (Sandbox Code Playgroud)