繁体   English   中英

找不到合适的构造函数(Java)

[英]No Suitable Constructor Found (Java)

我正在尝试在这里编译程序,但是遇到一个小错误,错误是“找不到适合ComplexNumber(double)的构造函数”。 到目前为止,这是我的代码。

public class ComplexNumber extends ImaginaryNumber
{
    private double realCoefficient;

    public ComplexNumber ( )
    {
        super ( );
        this.realCoefficient = 1;
    }

    public ComplexNumber (double r, double i)
    {
        super (i);
        this.realCoefficient = r;
    }

    public ComplexNumber add (ComplexNumber another)
    {
        return new ComplexNumber (this.realCoefficient + another.realCoefficient); //the error at complile occurs here, right at new.
    }//More Codes

我曾经遇到过这个问题,那是因为我没有参数化的构造函数。 但是这次,我确实有一个。 所以我不知道这次是什么问题。

这是我的ImaginaryNumber的代码

public class ImaginaryNumber implements ImaginaryInterface
{
//Declaring a variable.
protected double coefficient;

//Default constructor.
public ImaginaryNumber( )
{
    this.coefficient = 1;
}

//Parameterized constructor.
public ImaginaryNumber(double number)
{
    this.coefficient = number;
}

//Adding and returing an imaginary number.
public ImaginaryNumber add (ImaginaryNumber another)
{
    return new ImaginaryNumber(this.coefficient + another.coefficient);
}//More Codes

我的ImaginaryNumber类工作正常。

Java正在寻找一个Just的构造函数:

public ComplexNumber(double d){
   //to-do
}

您将需要创建一个适合这些参数的构造函数。

add方法中,您试图将一个参数恰好传递给构造函数,但是只有具有0或2个参数的构造函数。

看来您仍然需要添加ComplexNumber的虚部,因此将虚部添加作为构造函数的第二个参数。

使用Imaginarycoefficient保护变量:

return new ComplexNumber (this.realCoefficient + another.realCoefficient,
                          this.coefficient + another.coefficient);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM