Java 从同一类中的方法调用构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19614423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:46:19  来源:igfitidea点击:

Calling a constructor from method within the same class

java

提问by Nathan

I'm new to java and I'm learning about creating object classes. One of my homework assignment requires that I call the constructor at least once within a method of the same object class. I'm getting an error that says The method DoubleMatrix(double[][]) is undefined for the type DoubleMatrix

我是 Java 新手,正在学习创建对象类。我的一项家庭作业要求我在同一对象类的方法中至少调用一次构造函数。我收到一个错误,说The method DoubleMatrix(double[][]) is undefined for the type DoubleMatrix

Here's my constructor:

这是我的构造函数:

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

Here's the method where I try and call the constructor:

这是我尝试调用构造函数的方法:

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

Can someone point me to the right direction and explain why I'm getting an error?

有人可以指出我正确的方向并解释为什么我会出错吗?

采纳答案by Vikram

Can someone point me to the right direction and explain why I'm getting an error?

有人可以指出我正确的方向并解释为什么我会出错吗?

The reason is .. you are not declaring your object correctly. As few answers pointed out, you need to give a keyword called new. This newkeyword creates a new object for the class DoubleMatrixin Heap Memory.

原因是.. 你没有正确声明你的对象。正如少数答案指出的那样,您需要提供一个名为new. 该new关键字为DoubleMatrix堆内存中的类创建一个新对象。

else { newMatrix = new double[0][0]; new DoubleMatrix(newMatrix) }

Hope this helps

希望这可以帮助

回答by 0x6C38

You can use this()to call the constructor from inside another constructor(or super()to call the parent class constructor) but you can't the constructor from another method. Maybe you meant to create a new object? If so, just use new Object();. The constructor will be called for the new object.

您可以使用this()另一个构造函数内部调用构造函数(或super()调用父类构造函数),但不能从另一个方法调用构造函数。也许您打算创建一个新对象?如果是这样,只需使用new Object();. 将为新对象调用构造函数。

回答by Evgeniy Dorofeev

You cannot call constructors from methods, you can call constructors only from other constructors using this or super keywords. You can only call a constructor once, and it has to be the first statement in your constructor body. If you do not call any constructor from a contructore body java compiler will implicitly insert super() statement into the constructor

您不能从方法中调用构造函数,您只能使用 this 或 super 关键字从其他构造函数调用构造函数。您只能调用一次构造函数,并且它必须是构造函数主体中的第一条语句。如果不从构造函数体调用任何构造函数,java 编译器会隐式地将 super() 语句插入到构造函数中