【问题标题】:Incompatible types Void and Object - Java Generics不兼容的类型 Void 和 Object - Java 泛型
【发布时间】:2020-04-11 15:20:59
【问题描述】:

下面的代码抛出

"Incompatible types. Required Sample<Void> but create was inferred to Sample<T>. Incompatible equality constraints:Void and Object". 

public class SampleClass{
  public static <T> Sample<T> create(String str) {}
}

Sample<Void> sample = SampleClass.create("abc");

【问题讨论】:

标签: java generics


【解决方案1】:

它确实有效。

完整示例:

public class SampleClass {

    public static void main(String[] args) {
        Sample<Void> sample = SampleClass.create("abc");

    }

    public static <T> Sample<T> create(String str) {
        return null;
    }

    private static class Sample<T> {
    }
}

编译和执行都很好(使用 JDK 11)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2015-08-11
    • 2022-01-11
    • 2018-08-16
    相关资源
    最近更新 更多