Jas*_*n S 4 java generics compiler-errors
这让我很困惑.以下在Eclipse下编译正常.
package com.example.gotchas;
public class GenericHelper1 {
static <T> T fail() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
/**
* just calls fail()
* @return something maybe
*/
public boolean argh() { return fail(); }
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试使用ant或在命令行上进行干净的构建javac,我会得到:
src\com\example\gotchas\GenericHelper1.java:14: type parameters of <T>T cannot be determined; no unique maximal instance
exists for type variable T with upper bounds boolean,java.lang.Object
public boolean argh() { return fail(); }
^
1 error
Run Code Online (Sandbox Code Playgroud)
什么给出,我该如何解决?
两个编译器之间存在不一致.我发现了类似的错误,有时在Eclipse中,有时在JDK中.
在这种情况下,我不确定哪个是错的.问题似乎是由于泛型和自动装箱的结合.
在任何情况下,如果您明确指定type参数,而不是依赖于类型推断,它将编译:
public boolean argh() {
return GenericHelper.<Boolean>fail();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3176 次 |
| 最近记录: |