【发布时间】:2018-03-28 17:05:14
【问题描述】:
当我尝试为“CheckingAccount”类创建一个新的withdraw 方法时,由于某种原因,我收到了这个错误。我还有一个名为 Account 的类,它有自己的withdraw 方法。
代码如下:
class CheckingAccount extends Account {
double overdraftmax = -50;
public CheckingAccount(int id, double balance) {
}
public void withdraw(double money) {
if (this.getBalance() - money >= overdraftmax) {
withdraw(money);
}
}
}
class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private java.util.Date dateCreated;
Account() {
dateCreated = new java.util.Date();
}
Account(int newId,double newBalance) {
this();
id = newId;
balance = newBalance;
}
int getId() {
return id;
}
double getBalance() {
return balance;
}
double getAnnualInterestRate() {
return annualInterestRate;
}
void setId(int newId) {
id = newId;
}
void setBalance(double newBalance) {
balance = newBalance;
}
void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
String getDateCreated() {
return dateCreated.toString();
}
double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
double withdraw(double money) {
return balance -= money;
}
double deposit(double money) {
return balance += money;
}
}
这是我遇到的两个错误。
返回类型与Account.withdraw(double)不兼容
覆盖 Account.withdraw
我不确定要解决什么问题。
【问题讨论】:
-
那么,错误信息中有什么不清楚的地方?
-
你用另一个返回
void的方法覆盖了一个返回double的方法,这是不允许的。要么让超类返回void(恕我直言),要么在覆盖方法中返回double值 -
@AlexSavitsky 这行得通,谢谢!