在接口中所有方法都依赖与一个方法,
实现类中只需要重写这个方法就相当于重写了所有方法,所有方法都可以调用
//在接口中定义多个方法
trait FatLogger {
def log(msg:String)
def info(msg:String):Unit = log("info:"+msg)
def severe(msg:String):Unit = log("severe:"+msg)
def warn(msg:String):Unit = log("warn:"+msg)
}
class Account1 {
protected var balance:Double = 0.0
}
class SavingAccount1 extends Account1 with FatLogger {
//重写一个方法等于重写多个方法
override def log(msg: String): Unit = println(msg)
def withdraw(amount:Double):Unit = {
if(amount>balance)
severe("没有重写这个方法也可以调用")
else{
balance -= amount
info("钱已经减少")
}
}
}
object Testmain{
def main(args: Array[String]): Unit = {
val account = new SavingAccount1()
account.withdraw(1000)
}
}