Java黑皮书课后题第11章:11.3(Account类的子类)
时间: 2023-11-14 17:04:59 浏览: 250
题目描述:编写Account类的子类,名为CheckingAccount,该类继承Account类。该类必须包括一个名为overdraftProtection的double型数据域,该域存储与账户关联的可透支额。如果取款数超过账户余额和可透支额,则应打印一条消息,指出用户透支了多少美元。
解题思路:
1. CheckingAccount类继承Account类
2. CheckingAccount类包括一个名为overdraftProtection的double型数据域
3. 在取款方法中,如果取款数超过账户余额和可透支额,则打印透支消息,并显示透支金额
代码示例:
```
public class CheckingAccount extends Account {
private double overdraftProtection;
public CheckingAccount(double balance, double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
}
public double getOverdraftProtection() {
return overdraftProtection;
}
public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
}
@Override
public void withdraw(double amount) {
if (amount <= balance + overdraftProtection) {
balance -= amount;
} else {
System.out.println("Sorry, you do not have enough money to withdraw $" + amount);
System.out.println("Your balance is $" + balance + " and your overdraft protection is $" + overdraftProtection);
System.out.println("Your maximum withdraw limit is $" + (balance + overdraftProtection));
}
}
}
```
阅读全文