java - 泛型类型参数和方法调用

标签 java

我有带有类型参数 I 和 D 的 I_class:

class I_class<I extends I_class<I,D>, D extends D_class<I,D>>

我还有具有相同类型参数的 X_class:

class X_class<I extends I_class<I,D>, D extends D_class<I,D>>

X_class 有方法:

public D produceDeliver(I item) {...}

但是当我尝试从 I_class 调用此方法时:

X_class<I, D> x_object = ...    

public D produceDeliver(){ 
         D deliver = x_object.produceDeliver(this);
         ...

我收到此错误消息:

The method produceDeliver(I) in the type X_class< I,D > is not applicable for the arguments (I_class< I,D >)

我不明白 I 和 I_class< I,D > 之间的不等价性,因为 I 扩展了 I_class< I,D >

最佳答案

具有更具体名称的示例可能会有所帮助:

class Building<B extends Building<B, X>, X extends Thing<B, X>> {
  Shop<B,X> s = new Shop<>();
  X makesError = s.getLocation(this);
}
class Shop<S extends Building<S, X>, X extends Thing<S, X>> {
   public X get_location(S shop) {...}
}

问题在于,当您调用 getLocation 时,您正在传递 Building 类型的参数。并非 Building 类型的每个实例都是 Shop 类型。

关于java - 泛型类型参数和方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796882/

相关文章:

java - MySQL 更新语句不起作用

java - 如何在 CQ 6 中通过 LDAP 对目录进行用户身份验证

java - 如果 servlet 上下文初始化失败,则跳过部署或停止 Web 应用程序

java - 如何将总价添加到最终输出中?

java - 将 DefaultMutableTreeNode 值设置为默认值,用于 Spark mapToPair 时

java - 如何使用 java api 执行 BULK UpdateByQuery - Elasticsearch 5.x

java - 通用类型 x 通用参数 : Building a "very generic" structure

java - Spring Boot 应用程序不会 Autowiring 字段

javac : file not found even after settinh HOME_PATH

java - 为什么我会得到这个奇怪的输出 Hand@2802cf63?