8 继承
8.1 扩展如下的BankAccount类,新类CheckingAccount对每次存款和取款都收取1美元的手续费class BankAccount(initialBalance:Double){
private var balance = initialBalance
def deposit(amount:Double) = { balance += amount; balance}
def withdraw(amount:Double) = {balance -= amount; balance}
}
继承语法的使用。代码如下
- class CheckingAccount(initialBalance:Double) extends BankAccount(initialBalance){
- override def deposit(amount: Double): Double = super.deposit(amount - 1)
- override def withdraw(amount: Double): Double = super.withdraw(amount + 1)
- }
8.2 扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数。
- class SavingsAccount(initialBalance:Double) extends BankAccount(initialBalance){
- private var num:Int = _
- def earnMonthlyInterest()={
- num = 3
- super.deposit(1)
- }
- override def deposit(amount: Double): Double = {
- num -= 1
- if(num < 0) super.deposit(amount - 1) else super.deposit(amount)
- }
- override def withdraw(amount: Double): Double = {