实验:继承&super
程序员文章站
2024-02-26 22:02:28
...
免责声明
此项目为尚硅谷官网提供的学习资料,转载仅为方便学习,官方链接
题目要求
项目实现
我的答案
参考答案
package com.atguigu.exer2;
public class Account {
private int id;//账号
private double balance;//余额
private double annualInterestRate;//年利率
public Account(int id, double balance, double annualInterestRate) {
super();
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//返回月利率
public double getMonthlyInterest(){
return annualInterestRate / 12;
}
//取钱
public void withdraw (double amount){
if(balance >= amount){
balance -= amount;
return;
}
System.out.println("余额不足");
}
//存钱
public void deposit (double amount){
if(amount > 0){
balance += amount;
}
}
}
package com.atguigu.exer2;
public class CheckAccount extends Account {
private double overdraft;// 可透支限额
public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
@Override
public void withdraw(double amount) {
if (getBalance() >= amount) {// 余额就足够消费
// getBalance() -= amount;
// 方式一:
// setBalance(getBalance() - amount);
// 方式二:
super.withdraw(amount);
} else if (overdraft >= amount - getBalance()) {// 透支额度+余额足够消费
overdraft -= (amount - getBalance());
// setBalance(0);
// 或
super.withdraw(getBalance());
} else {
System.out.println("超过可透支限额!");
}
}
}
上一篇: 基于Spring开发之自定义标签及其解析
下一篇: 卡片游戏 [逆序对_隐 II]