【发布时间】:2017-07-20 16:08:58
【问题描述】:
我正在为一个银行帐户建模,其中包含一个不能透支的超类 Account 和一个子类 SavingsAccount。 makeWithdrawal()方法,在主类调用时,应检查提款是否大于余额并提示输入,然后编辑余额。
如何从Account 调用makeWithdrawal() 方法并在SavingsAccount 中使用super 关键字覆盖它?我的编译器给了我“错误:不兼容的类型:缺少返回值。
Account中的方法:
double makeWithdrawal(double withdrawal) {
return balance -= withdrawal;
}
(很简单。)这个方法最初是抽象的,但它会导致错误。
SavingsAccount中的方法:
public double makeWithdrawal(double withdrawal) {
double tempbalance = getBalance();
if (withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
return;
}
else {
return super.makeWithdrawal(withdrawal);
}
}
【问题讨论】:
-
super.makeWithdrawal() -
我在 else() 中的 SavingsAccount 中有 super.makeWithdrawal()。
-
当你这样做时会发生什么?
-
OP,你需要指出错误发生在哪里。答案比你说的要简单得多。
标签: java inheritance subclass superclass bank