小编ebe*_*ger的帖子

Java从方法返回类型派生的泛型类型推断

public class TestCase {
    public static String compiles() {
        return getAction();
    }

    /*
    public static String doesntCompile1() {
        return (String) getAction();
    }

    public static String doesntCompile2() {
        return TestCase.<String> getAction();
    }
    */

    public static <T extends javax.swing.Action> T getAction() {
        return (T) null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该被视为javac v1.6.0_22和jdt v3.7.1中的错误吗?对我来说似乎是这样,因为在其他情况下,编译器实际上发现了潜在的ClassCastException.在compiles方法中,这将在运行时抛出ClassCastException.

带来这个例子的原始代码没有在javac中编译,打印出以下异常.不幸的是,我出于某种原因提供的例程代码不会产生此错误.

type parameters of <T>T cannot be determined;
no unique maximal instance exists for type variable T with upper bounds
Run Code Online (Sandbox Code Playgroud)

java generics type-inference javac eclipse-jdt

5
推荐指数
1
解决办法
1859
查看次数

标签 统计

eclipse-jdt ×1

generics ×1

java ×1

javac ×1

type-inference ×1