[英]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
的虛部,因此將虛部添加作為構造函數的第二個參數。
使用Imaginary
的coefficient
保護變量:
return new ComplexNumber (this.realCoefficient + another.realCoefficient,
this.coefficient + another.coefficient);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.