- 2012/3/11
class Account(val id: String, val name: String) {
private var bal: Int = _
def deposit(amount: Int) {
require(amount > 0) // 不能存負數
bal += amount
}
def withdraw(amount: Int) {
require(amount > 0) // 不能提負數
if(amount >= bal) {
bal -= amount
}
else {
throw new RuntimeException("餘額不足")
}
}
def balance = bal
}
class SavingAccount(val id: String, val name: String) extends Account {
protected var bal = 0
protected def balance_=(bal: Int) {
this.bal = bal
}
def withdraw(amount: Int) = {
require(amount > 0)
if(amount < bal) {
bal -= amount
}
else {
throw new RuntimeException("餘額不足")
}
}
}
if中的判斷,應該是 amount <= bal
2.
範例輸出結果不相符
class Some {
def apply(a: String) = a + " from apply...."
def apply(a: String, b: String) = a + ", " + b + " from apply..."
def update(a: String, b: String) = a + ", " + b + " from update..."
def update(a: String, b: String, c: String) =
a + ", " + b + ", " + c + " from update..."
}
val some = new Some()
println(some("data1")) // data1 from apply....
println(some("data1", "data2")) // data1, data2 from apply...
println(some("data3") = "data4") // data3, data4 from update...
println(some("data3", "data4") = "data5") // data3, data4, data5 from update...