gpt4 book ai didi

java - 处理泛型和继承时出现绑定(bind)不匹配错误

转载 作者:行者123 更新时间:2023-11-29 08:34:36 25 4
gpt4 key购买 nike

我有以下类和接口(interface):

public class Entity {}
public interface EntitySet<T extends Entity> {}

现在我想编写实现 EntitySet 接口(interface)并重用类型 T 的类。我试过以下:

public class Method1EntitySet<T> implements EntitySet<T extends Entity>{}

这给了我错误:

Syntax error on token "extends", , expected

所以我尝试了:

public class Method1EntitySet<T> implements EntitySet<T>{}

这给我错误:

Bound mismatch: The type T is not a valid substitute for the bounded parameter of the type EntitySet

这个有效:

public class Method1EntitySet<T> implements EntitySet{} 

但它给了我警告:

EntitySet is a raw type. References to generic type EntitySet should be parameterized.

另外我猜,上面的 T 没有被强制扩展 Entity

我应该如何完成这项工作?

最佳答案

类型绑定(bind)应该在声明泛型类型参数 T 的地方:

public class Method1EntitySet<T extends Entity> implements EntitySet<T>{}

关于java - 处理泛型和继承时出现绑定(bind)不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45143017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com