小编Tim*_*nov的帖子

为什么这在Java7中编译而在Java8中不编译?

泛型是棘手的.看起来它们在不同版本的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)

java generics compiler-errors java-7 java-8

8
推荐指数
1
解决办法
2119
查看次数

标签 统计

compiler-errors ×1

generics ×1

java ×1

java-7 ×1

java-8 ×1