为什么jGrasp没有意识到我的构造函数需要两个双打?特别是,因为我只有一个构造函数。
这是我的班级:
public class SavingsAccount{
private double balance;
private double interestRate;
private double lastInterest;
public void SavingsAccount(double bal, double intRate){
balance = bal;
interestRate = intRate;
}
//Other methods
}
在我的main方法中,我尝试实例化它:
double bal = 500.00;
double intRate = .002;
SavingsAccount savings = new SavingsAccount(bal, intRate);
但是当我尝试运行这是jGrasp时,它会显示一个错误,即
TestSavingsAccount.java:9: error: constructor SavingsAccount in class SavingsAccount cannot be applied to given types;
SavingsAccount savings = new SavingsAccount(bal, intRate);
^
required: no arguments
found: double,double
reason: actual and formal argument lists differ in length
1 error
答案 0 :(得分:3)
你的“构造函数”有一个void
字,这使它成为一种方法。构造函数不能具有返回类型。你需要删除它:
public SavingsAccount(double bal, double intRate){
balance = bal;
interestRate = intRate;
}
答案 1 :(得分:2)
public void SavingsAccount(double bal, double intRate){
balance = bal;
interestRate = intRate;
}
应该是:
public SavingsAccount(double bal, double intRate){
balance = bal;
interestRate = intRate;
}
您声明了一个向签名添加void的方法。