我有一个具有此定义的泛型类:
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
不被接受为有效参数?
答案 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
条款。