实验-继承&super.doc
Account.java
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; } } }
Account.java
package com.atguigu.exer2; /* * 写一个用户程序测试Account类。在用户程序中, * 创建一个账号为1122、余额为20000、年利率4.5%的Account对象。 * 使用withdraw方法提款30000元,并打印余额。 * 再使用withdraw方法提款2500元, * 使用deposit方法存款3000元,然后打印余额和月利率。 * */ public class AccountTest { public static void main(String[] args) { Account acct = new Account(1122, 20000, 0.045); acct.withdraw(30000); System.out.println("您的账户余额为:" + acct.getBalance()); acct.withdraw(2500); System.out.println("您的账户余额为:" + acct.getBalance()); acct.deposit(3000); System.out.println("您的账户余额为:" + acct.getBalance()); System.out.println("月利率为:" + (acct.getMonthlyInterest() * 100) +"%"); } }
CheckAccount.java -
package com.atguigu.exer2; /* * 创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。 * 在CheckAccount类中重写withdraw方法,其算法如下: 如果(取款金额<账户余额), 可直接取款 如果(取款金额>账户余额), 计算需要透支的额度 判断可透支额overdraft是否足够支付本次透支需要,如果可以 将账户余额修改为0,冲减可透支金额 如果不可以 提示用户超过可透支额的限额 * * */ 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("超过可透支限额!"); } } }
CheckAccountTest.java
package com.atguigu.exer2; /* * 写一个用户程序测试CheckAccount类。 * 在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。 使用withdraw方法提款5000元,并打印账户余额和可透支额。 再使用withdraw方法提款18000元,并打印账户余额和可透支额。 再使用withdraw方法提款3000元,并打印账户余额和可透支额。 * */ public class CheckAccountTest { public static void main(String[] args) { CheckAccount acct = new CheckAccount(1122, 20000, 0.045, 5000); acct.withdraw(5000); System.out.println("您的账户余额为:" + acct.getBalance()); System.out.println("您的可透支额度为:" + acct.getOverdraft()); acct.withdraw(18000); System.out.println("您的账户余额为:" + acct.getBalance()); System.out.println("您的可透支额度为:" + acct.getOverdraft()); acct.withdraw(3000); System.out.println("您的账户余额为:" + acct.getBalance()); System.out.println("您的可透支额度为:" + acct.getOverdraft()); } }