我有一个具有此定义的通用类:
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
不被接受为有效参数? 最佳答案
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
子句。