1、创建Account类
package test2;
/**
* 〈一句话功能简述〉<br>
* 〈实验类的继承〉
*
* @author abu
* @create 2019/7/14
* @since 1.0.0
*/
public class Account {
private int id;
protected double balance;
private double annualIntersetRate;
public Account() {
}
public Account(int id, double balance, double annualIntersetRate){
//super();
this.id = id;
this.balance = balance;
this.annualIntersetRate = annualIntersetRate;
}
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 getAnnualIntersetRate() {
return annualIntersetRate;
}
public void setAnnualIntersetRate(double annualIntersetRate) {
this.annualIntersetRate = annualIntersetRate;
}
public double getMonthlyInterest(){
return annualIntersetRate/12;
}
public void withdraw(double amount){
if(amount <= this.balance){
balance -= amount;
}else{
System.out.println("没钱还来取!");
}
}
public void deposit(double amount){
balance += amount;
}
}
2、测试Account类
public class TestAmount {
public static void main(String[] args){
//Account a = new Account();
Account a = new Account(1122, 20000, 0.045);
System.out.println(a.getId() + "的余额为:" + a.getBalance() +
"月利率为: " + a.getMonthlyInterest());
a.withdraw(30000);
System.out.println(a.getBalance());
a.withdraw(2500);
a.deposit(3000);
System.out.println(a.getId() + "的余额为:" + a.getBalance()
+ "月利率为: " + a.getMonthlyInterest());
}
}
3、Check Account类
public class CheckAccount extends Account{
protected double overdraft;
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public CheckAccount(int id, double balance, double annualIntersetRate, double overdraft) {
super(id, balance, annualIntersetRate);
this.overdraft = overdraft;
}
public void withdraw(double amount){
if( balance >= amount ){
balance -= amount;
}
else if(overdraft >= (amount - balance)){
overdraft -= (amount - balance);
balance = 0;//这两行顺序不可以调换,因为balance赋值为0之后,
// 下边进行减的运算就没用了,搞了半天我才发现这个问题!!!
}
else{
System.out.println("没钱还来取!透支也透支不了了!");
}
}
}
4、测试Check Account类
public class TestCheckAccount {
public static void main(String[] args) {
CheckAccount ca = new CheckAccount(1, 20000, 0.045, 5000);
ca.withdraw(18000);
System.out.println(ca.getId() + "的余额为:" + ca.getBalance()
+ "月利率为: " + ca.getMonthlyInterest() + "透支额度还有:" + ca.getOverdraft());
ca.withdraw(3000);
System.out.println(ca.getId() + "的余额为:" + ca.getBalance()
+ "月利率为: " + ca.getMonthlyInterest() + "透支额度还有:" + ca.getOverdraft());
ca.withdraw(5000);
System.out.println(ca.getId() + "的余额为:" + ca.getBalance()
+ "月利率为: " + ca.getMonthlyInterest() + "透支额度还有:" + ca.getOverdraft());
}
}