java - 不兼容的类型 Void 和 Object - Java 泛型
问题描述
下面的代码抛出
"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");
解决方案
它确实有效。
完整示例:
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)。
推荐阅读
- python - 有没有办法从 kubernetes pod 中列出主机上的文件?
- python - 如何修复mysqlclient问题
- spring-boot - 此 iss 声明不等于配置的颁发者。Spring Security Oauth2
- c++ - g++ 尝试从内核访问 0x00 时生成未定义指令(ud2)
- java - 为什么在 JPA 中使用“select new”将实体放入 POJO 类会导致 N + 1 问题?
- git - 如何在不删除的情况下撤消错误提交
- c++ - 字符串向量打印成 int?
- iot - 如何使用我的品牌配置添加对 esp8266 的 alexa 支持?
- ruby - 执行 `zypper update` 后 yast2 损坏
- pandas - Spyder 无法从控制台检索此变量的值。错误消息是:“元组”对象没有属性“raise_error”