以下代码
public class GenericsTest2 {
public static void main(String[] args) throws Exception {
Integer i = readObject(args[0]);
System.out.println(i);
}
public static <T> T readObject(String file) throws Exception {
return readObject(new ObjectInputStream(new FileInputStream(file)));
// closing the stream in finally removed to get a small example
}
@SuppressWarnings("unchecked")
public static <T> T readObject(ObjectInputStream stream) throws Exception {
return (T)stream.readObject();
}
}
在 Eclipse 中编译,但不使用 javac 编译(无法确定 T 的类型参数;对于具有上限 T,java.lang.Object 的类型变量 T 不存在唯一的最大实例)。
当我将 readObject(String file) 更改为
@SuppressWarnings("unchecked")
public static <T> T readObject(String file) throws Exception {
return (T)readObject(new ObjectInputStream(new FileInputStream(file)));
}
它在 eclipse 和 javac 中编译。谁是正确的,eclipse编译器还是javac?
最佳答案
我会说这是 Sun 编译器中报告的错误 here和 here ,因为如果您将行更改为下面的行,则两者都适用,这似乎正是错误报告中描述的内容。
return GenericsTest2.<T>readObject(new ObjectInputStream(new FileInputStream(file)));
关于java - eclipse 编译器或 javac ("type parameters of T cannot be determined"中的错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/314572/