我定义了两个类:
public abstract class Problem {
double[] tempResponseSet;
double[][] tempLocation;
protected DesignSpace designSpace = null;
protected EvalStruct evalStruct = null; protected Problem(int Num1,int Num2, int Num) throws Exception {
designSpace = new DesignSpace(Num1,Num2);
evalStruct = new EvalStruct(targetNum);
tempLocation = new double[Num1][Num2];
tempResponseSet = new double[Num];
}
}子类:
public class TaskPlant extends Problem {
public TaskPlant() throws Exception { super(3,3,4);//定义数组3行3列,4个约束 //变量的取值范围
for (int i = 0; i < 3; i++) {
for (int j = 0; j <3; j++) {
setXAt(i, j, 0, 1);
}
}
}在子类TaskPlant中使用super(3,3,4),前两个数分别表示:我定义的数组的行数和列数,调用父类的Problem,起到规定一个范围的作用。现在的问题是:
如果数组是一个定行定列的,即行数m和列数n是定数的话,我利用super没有问题,但是我现在需要定义的数组是不规则的数组,不规则的数组行数是确定的,但是列数没法确定阿,而super()又必须放在子类的首句来说明,这该怎么办啊?
public abstract class Problem {
double[] tempResponseSet;
double[][] tempLocation;
protected DesignSpace designSpace = null;
protected EvalStruct evalStruct = null; protected Problem(int Num1,int Num2, int Num) throws Exception {
designSpace = new DesignSpace(Num1,Num2);
evalStruct = new EvalStruct(targetNum);
tempLocation = new double[Num1][Num2];
tempResponseSet = new double[Num];
}
}子类:
public class TaskPlant extends Problem {
public TaskPlant() throws Exception { super(3,3,4);//定义数组3行3列,4个约束 //变量的取值范围
for (int i = 0; i < 3; i++) {
for (int j = 0; j <3; j++) {
setXAt(i, j, 0, 1);
}
}
}在子类TaskPlant中使用super(3,3,4),前两个数分别表示:我定义的数组的行数和列数,调用父类的Problem,起到规定一个范围的作用。现在的问题是:
如果数组是一个定行定列的,即行数m和列数n是定数的话,我利用super没有问题,但是我现在需要定义的数组是不规则的数组,不规则的数组行数是确定的,但是列数没法确定阿,而super()又必须放在子类的首句来说明,这该怎么办啊?
如果想达到你想的效果,那你可以在父类中重写个构造方法,参数中没有控制列的。
然后在构造方法中这样:
super(3, getColumns(...), 4);
Object[][] objs = null;
objs = new Object[3][];
objs[0] = new Object[5];
objs[1] = new Object[4];
objs[2] = new Object[3];还可以在循环中创建列,只要你给一个列数的规则就行
是这个意思,
如果一个矩阵为
c={{1,2,3},{1,3,4},{6,3,5}}
我可以super为:super(3,3,4)//3行3列,4个约束
如果c={{1,2},{1,3,4},{6,3,5}}这种3行,第一行2列,第二行3列,第三行3列,
这个super应该怎么办?