相关疑难解决方法(0)

eclipse编译器或javac中的错误("T的类型参数无法确定")

以下代码

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在eclipse中编译,但不在javac中编译(T的类型参数无法确定;对于具有上限T,java.lang.Object的类型变量T,不存在唯一的最大实例).

当我将readObject(String文件)更改为

    @SuppressWarnings("unchecked")
    public static <T> T readObject(String file) throws Exception {
        return (T)readObject(new ObjectInputStream(new FileInputStream(file)));
    }
Run Code Online (Sandbox Code Playgroud)

它在eclipse和javac中编译.谁是正确的,eclipse编译器还是javac?

java eclipse generics

74
推荐指数
2
解决办法
3万
查看次数

标签 统计

eclipse ×1

generics ×1

java ×1