Java泛型:绑定不匹配

时间:2015-07-15 23:08:07

标签: java generics

我有一个具有此定义的泛型类:

public class AcoProblemSolver<C, E extends Environment, A extends AntColony<E, Ant<C, E>>> {

AntColony这样的地方:

public abstract class AntColony<E extends Environment, A extends Ant<?, E>> {

而且Ant是这样的:

public abstract class Ant<C, E extends Environment> {

我希望以这种方式扩展AntColony

public class FlowShopProblemSolver extends
    AcoProblemSolver<Integer, FlowShopEnvironment, FlowShopAntColony> {

但Eclipse在FlowShopAntColony参数类上显示错误:

Bound mismatch: The type FlowShopAntColony is not a valid substitute for the bounded parameter <A extends AntColony<E,Ant<C,E>>> of the type AcoProblemSolver<C,E,A>

这使我感到困惑,因为FlowShopAntColony是这样定义的:

public class FlowShopAntColony extends
    AntColony<FlowShopEnvironment, AntForFlowShop> {

而且AntForFlowShop是这样的:

public class AntForFlowShop extends Ant<Integer, FlowShopEnvironment> {

为什么FlowShopAntColony不被接受为有效参数?

1 个答案:

答案 0 :(得分:8)

A extends AntColony<E, Ant<C, E>>

AcoProblemSolver的第三个参数具有限制extends AntColony<E, Ant<C, E>>AntColony的第二个参数必须完全 Ant<C, E>,并且您尝试传递Ant的子类。尝试:

A extends AntColony<E, ? extends Ant<C, E>>

您可能希望在其他地方使用其他类似的? extends条款。