Java黑皮书课后题第11章:11.3(Account类的子类)在编程练习题9.7中定义了一个Account类来对一个银行账户建模。一个账户有账号、余额、年利率、开户日期等属性,以及存款和取款等方法
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第11章:11.3(Account类的子类)在编程练习题9.7中定义了一个Account类来对一个银行账户建模。一个账户有账号、余额、年利率、开户日期等属性,以及存款和取款等方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
續標題:創建支票賬戶checking account和儲蓄賬戶saving account兩個子類。支票賬戶有一個透支限定額,但儲蓄賬戶不能透支
- 題目(續)
- 題目中提到的編程練習題9.7:以下代碼直接利用即可
- Test03_checking_account:支票賬戶
- Test03_saving_account
- Test03:測試程序(創建三個對象并調用toString方法)
- 總UML圖
題目(續)
畫出這些類的UML圖并實現這些類。編寫一個測試程序,創建Account、SavingsAccount和CheckingAccount的對象,然后調用它們的toString()方法
題目中提到的編程練習題9.7:以下代碼直接利用即可
省流助手:四個私有數據域 + 無參有參構造方法 + id balance annualInterestRate三個數據域的setter和getter方法 + dateCreated的訪問器方法 + getMonthlyInterestRate方法 + getMonthlyInterest方法 + withDraw方法 + deposit方法
import java.util.Date;public class Test2_Account {// 四個私有數據域private int id = 0;private double balance = 0.0;private double annualInterestRate = 0.0;private Date dateCreated;// 無參構造方法public Test2_Account(){}// 有參構造方法public Test2_Account(int id, double balance){this.id = id;this.balance = balance;}// id balance annualInterestRate的setter和getterpublic 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;}// dateCreated的訪問器方法public Date getDateCreated(){return dateCreated;}// getMonthlyInterestRate方法public double getMonthlyInterestRate(){return annualInterestRate / 1200;}// getMonthlyInterest方法public double getMonthlyInterest(){return annualInterestRate * balance / 1200;}// withDraw方法public void withDraw(double num){if (num <= balance) balance -= num;}// deposit方法public void deposit(double num){balance += num;}@Overridepublic String toString() {return "Test03_Account{" +"id=" + id +", balance=" + balance +", annualInterestRate=" + annualInterestRate +", dateCreated=" + dateCreated +'}';} }本類UML圖:
Test03_checking_account:支票賬戶
public class Test03_checking_account extends Test03_Account{public double overDraftLimit = 0;public Test03_checking_account(){}public Test03_checking_account(double overDraftLimit){this.overDraftLimit = overDraftLimit;}@Overridepublic String toString() {return "Test03_checking_account{" +"overDraftLimit=" + overDraftLimit +"} " + super.toString();} }Test03_saving_account
public class Test03_saving_account extends Test03_Account{private double minBalance = 0.0; }Test03:測試程序(創建三個對象并調用toString方法)
public class Test03 {public static void main(String[] args) {// 創建AccountTest03_Account ta = new Test03_Account();ta.toString();// 創建Savings-AccountTest03_saving_account sa = new Test03_saving_account();sa.toString();// 創建CheckingAccountTest03_checking_account ca = new Test03_checking_account();ca.toString();} }總UML圖
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Java黑皮书课后题第11章:11.3(Account类的子类)在编程练习题9.7中定义了一个Account类来对一个银行账户建模。一个账户有账号、余额、年利率、开户日期等属性,以及存款和取款等方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第11章:11.2(
- 下一篇: python安装opencv出现错误,通