1. 改进5.1的Counter类,让它不要再Int.MaxValue时变成负数
class Counter{
private var value = 0
def increment()={
if(value ==Int.MaxValue) value.toLong
value += 1
}
def current =value
}
2. 编写一个BankAccount类,加入deposit和withdraw方法,和一个只读的balance属性。
class BankAccount{
private var balance = 0.0
//存钱
def deposit(money:Double)={
balance += money
}
//qiu钱
def withDraw(money:Double)={
if(balance >= money) {
balance -=money
}else{
throw new Exception("money not enough")
}
}
//查询余额
def getBalance = balance
}
3. 编写个Time类,加入只读属性hours和minutes,和一个检查某一时刻是否早于另一时刻的方法before(other:Time):Boolean。Time对象应该以new Time(hrs,min)方式构建,其中hrs小时的格式是以军用时间格式呈现的(介于0和23之间)
class MyTime( h :Int =0, m:Int = 0){
private var hours:Int=0
private var minutes:Int = 0
//时间逻辑校验
if(h>= 0 & h <=23){
this.hours=h
}else{ throw new Exception("consture time error")}
if(m >=0 & m<=59){
this.minutes = m
}else{throw new Exception("consture time error")}
//判断时间是否早于当前
def befor(other:MyTime)={
if(this.hours > other.hours) true
else if (this.hours ==other.hours & this.minutes >other.minutes) true
else false
}
//获取时间
def getHours =this.hours
def getMinutes = this.minutes
4. 重新实现前一个练习中的Time类,将内部呈现改成自午夜起的分钟数(介于 0 到 24*60-1之间)。不要改变公有接口。也就是说,客户端代码不应因为你的修改而受到影响。
class MyTime2(var h:Int=0,var m :