从子类调用超类方法
Calling a superclass method from a subclass
我正在用一个不能透支的 superclass Account
和 subclass SavingsAccount
模拟一个银行账户。 makeWithdrawal()
方法,当从主 class 调用时,应该检查取款是否大于余额并提示输入,然后编辑余额。
如何从 Account
调用 makeWithdrawal()
方法并使用 super 关键字在 SavingsAccount
中覆盖它?我的编译器给我“错误:不兼容的类型:缺少 return 值。
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);
}
}
如果我理解正确的话:
Account 是基础 class 而 SavingsAccount 是子class?
然后:super.makeWithdrawal(withdrawal);
还要确保在定义 class 时继承:
public class SavingsAccount extends Account{...}
问题不在于您对 super.makeWithdrawl()
的调用,这是正确的。它与上面 if
子句中的空 return
语句一起使用。您获取提款金额更新的逻辑是明智的,但是您需要 return 执行此操作时的新余额。我建议将调用移至 super.makeWithdrawl()
条件之外:
if(withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
}
return super.makeWithdrawal(withdrawal);
如果原来的提款金额太大,这将使用更新后的提款金额。
如果
if (withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
return;
}
您需要 return 双倍值而不是简单 returning。
问题出在
return;
您应该将其替换为
return withdrawal;
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 withdrawal;
}
else {
return super.makeWithdrawal(withdrawal);
}
}
我正在用一个不能透支的 superclass Account
和 subclass SavingsAccount
模拟一个银行账户。 makeWithdrawal()
方法,当从主 class 调用时,应该检查取款是否大于余额并提示输入,然后编辑余额。
如何从 Account
调用 makeWithdrawal()
方法并使用 super 关键字在 SavingsAccount
中覆盖它?我的编译器给我“错误:不兼容的类型:缺少 return 值。
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);
}
}
如果我理解正确的话:
Account 是基础 class 而 SavingsAccount 是子class?
然后:super.makeWithdrawal(withdrawal);
还要确保在定义 class 时继承:
public class SavingsAccount extends Account{...}
问题不在于您对 super.makeWithdrawl()
的调用,这是正确的。它与上面 if
子句中的空 return
语句一起使用。您获取提款金额更新的逻辑是明智的,但是您需要 return 执行此操作时的新余额。我建议将调用移至 super.makeWithdrawl()
条件之外:
if(withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
}
return super.makeWithdrawal(withdrawal);
如果原来的提款金额太大,这将使用更新后的提款金额。
如果
if (withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
return;
}
您需要 return 双倍值而不是简单 returning。
问题出在
return;
您应该将其替换为
return withdrawal;
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 withdrawal;
}
else {
return super.makeWithdrawal(withdrawal);
}
}