我是java的新手,我正在学习如何创建对象类。我的一个家庭作业要求我在同一个对象类的方法中至少调用一次构造函数。我收到一个错误,显示为The method DoubleMatrix(double[][]) is undefined for the type DoubleMatrix
下面是我的构造函数:
public DoubleMatrix(double[][] tempArray)
{
// Declaration
int flag = 0;
int cnt;
// Statement
// check to see if doubArray isn't null and has more than 0 rows
if(tempArray == null || tempArray.length < 0)
{
flag++;
}
// check to see if each row has the same length
if(flag == 0)
{
for(cnt = 0; cnt <= tempArray.length - 1 || flag != 1; cnt++)
{
if(tempArray[cnt + 1].length != tempArray[0].length)
{
flag++;
}
}
}
else if(flag == 1)
{
makeDoubMatrix(1, 1);// call makeDoubMatrix method
}
}// end constructor 2下面是我尝试调用构造函数的方法:
public double[][] addMatrix(double[][] tempDoub)
{
// Declaration
double[][] newMatrix;
int rCnt, cCnt;
//Statement
// checking to see if both are of same dimension
if(doubMatrix.length == tempDoub.length &&
doubMatrix[0].length == tempDoub[0].length)
{
newMatrix = new double[doubMatrix.length][doubMatrix[0].length];
// for loop to add matrix to a new one
for(rCnt = 0; rCnt <= doubMatrix.length; rCnt++)
{
for(cCnt = 0; cCnt <= doubMatrix.length; cCnt++)
{
newMatrix[rCnt][cCnt] = doubMatrix[rCnt][cCnt] + tempDoub[rCnt][cCnt];
}
}
}
else
{
newMatrix = new double[0][0];
DoubleMatrix(newMatrix)
}
return newMatrix;
}// end addMatrix method谁能给我指出正确的方向,并解释为什么我会得到一个错误?
发布于 2013-10-27 12:06:23
有人能给我指出正确的方向并解释为什么我会遇到错误吗?
原因是..您没有正确声明您的对象。正如一些答案指出的那样,您需要提供一个名为new的关键字。此new关键字在堆内存中为类DoubleMatrix创建一个新对象。
else { newMatrix = new double[0][0]; new DoubleMatrix(newMatrix) }希望这能有所帮助
发布于 2013-10-27 11:50:43
您可以使用this()从inside another constructor调用构造函数(或使用super()调用父类构造函数),但不能从另一个方法调用构造函数。也许你想创建一个新的对象?如果是这样的话,就使用new Object();。将为新对象调用构造函数。
发布于 2013-10-27 11:51:12
不能从方法调用构造函数,只能使用this或super关键字从其他构造函数调用构造函数。一个构造函数只能调用一次,而且必须是构造函数主体中的第一个语句。如果不从构造体调用任何构造函数,java编译器将隐式地将super()语句插入到构造函数中。
https://stackoverflow.com/questions/19614423
复制相似问题