[英]call extended class variable in parent in ECJ
I am using ECJ (an evolutionary algorithms package) and I want to call certain variables that are in an extended class but I can't figure out how to get to them from the problem class. 我正在使用ECJ(进化算法程序包),并且想调用扩展类中的某些变量,但是我不知道如何从问题类中获取它们。
As you can see in the problem class below I want to be able to call the variable x in the NetworkGene class but it doesn't work because I end up in the VectorGene class in which I can only call variable y. 正如您在下面的问题类中看到的那样,我希望能够在NetworkGene类中调用变量x,但是它不起作用,因为我最终只能在VectorGene类中调用变量y。
class Problem {
double fitness = 0;
public void evaluate(final Individual ind){
if(!(ind. instanceOf GeneVectorIndividual)){
state.output.fatal("Not a GeneVectorIndividual",null);
}
fitness = 0;
for(){
fitness += ind.genome[i].x;
}
}
}
public abstrabct class VectorIndividual extends Individual{
}
public class GeneVectorIndividual extends VectorIndividual{
VectorGene[] genome;
}
public abstract class VectorGene implements Prototype {
double y;
}
public class NetworkGene extends VectorGene{
double x;
}
Classic polymorphism problem. 经典多态性问题。
The quick and dirty solution is this: in the for
loop in class Problem
, add these lines 快速而肮脏的解决方案是:在类Problem
的for
循环中,添加以下行
if (ind.genome[i] instanceof NetworkGene) {
fitness += ((NetworkGene) ind.genome[i]).x;
}
But probably there's a design problem you need to solve: 但是可能需要解决一个设计问题:
Why is ind.genome
an array of VectorGenes instead of NetworkGenes? 为什么ind.genome
是ind.genome
而不是ind.genome
的数组?
What does x
represent? x
代表什么? Is there something you could express within VectorGene
like a method getSomeValue()
which in NetworkGene
you would implement to return x
(and in other subclasses you'd implement it to return some other appropriate value)? 有什么你可以表达内VectorGene
等的方法getSomeValue()
这NetworkGene
您将实施返回x
(和其他子类你实现它返回一些其他适当的值)?
Do you really need the inheritance relationship between VectorGene
and NetworkGene
- does it relate to some difference that you actually need to exploit in the problem you are currently trying to solve? 你真的需要之间的继承关系VectorGene
和NetworkGene
-它涉及到一些差别,你确实需要在目前你正在试图解决的问题利用? Couldn't you just have a single class containing properties x
and y
? 您是否只有一个包含属性x
和y
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.