Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java8需要强制转换,而Java7没有';t-enum.getClass/getDeclaringClass_Java_Generics_Java 8 - Fatal编程技术网

Java8需要强制转换,而Java7没有';t-enum.getClass/getDeclaringClass

Java8需要强制转换,而Java7没有';t-enum.getClass/getDeclaringClass,java,generics,java-8,Java,Generics,Java 8,我意识到Java 8仍处于测试阶段,但这一测试让我感到奇怪: public class Fields<C extends Enum<C>> { public Fields(Set<C> columns) { // A sample column used to find the universe of the enum of Columns. C sampleCol = columns.iterator().next(

我意识到Java 8仍处于测试阶段,但这一测试让我感到奇怪:

public class Fields<C extends Enum<C>> {

    public Fields(Set<C> columns) {
        // A sample column used to find the universe of the enum of Columns.
        C sampleCol = columns.iterator().next();
        // Java 8 needs a cast here.
        Set<C> allColumns = EnumSet.allOf((/*Class<C>)*/ sampleCol.getClass());
        // ... there's more to this that I've deleted.
    }

}
公共类字段{
公共字段(设置列){
//用于查找列枚举范围的示例列。
C sampleCol=columns.iterator().next();
//Java8需要在这里进行转换。
Set allColumns=EnumSet.allOf((/*Class)*/sampleCol.getClass());
//…我删除了更多的内容。
}
}
错误内容如下:

error: incompatible types: inferred type does not conform to equality constraint(s)
            Set<C> allColumns = EnumSet.allOf(sampleCol.getClass());
    inferred: C
    equality constraints(s): C,CAP#1
  where C is a type-variable:
    C extends Enum<C> declared in class Test.Fields
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Enum from capture of ? extends Enum
错误:不兼容的类型:推断的类型不符合相等约束
Set allColumns=EnumSet.allOf(sampleCol.getClass());
推断:C
平等约束:C,第1章
其中C是一个类型变量:
C扩展类Test.Fields中声明的枚举
其中CAP#1是一个新类型变量:
CAP#1从捕获扩展了枚举?扩展枚举

这是Java 8的一个bug还是一个新特性?

有趣的是,这是对Java 8的处理方式的一个细微变化

首先,让我们澄清一下你的例子。返回类型是特殊的:


实际的结果类型是
ClassI,我用Java7(51)和Java8(b128)进行了尝试,在这两种情况下,编译器都需要强制转换,当然在这两种情况下仍然处于警告状态。但当我删除强制转换时,在这两种情况下都会出现错误(Java7,8)。我在IDEA 13环境中尝试过。感谢您的见解和解决方案-非常有趣。奇怪的是,这是本周我第二次需要在完全不同的情况下使用
getDeclaringClass
。我以前从未需要过它。