abstractclassUndoableAction(val description:String){
def undo():Unit
def redo():Unit
}
object DoNothingAction extendsUndoableAction("Do Nothing"){
override def undo(): Unit ={}
override def redo(): Unit ={}}
val actions =Map("open"->DoNothingAction,"save"->DoNothingAction)
Apply方法
Object(參數1,參數2,…,參數n) 會調用對象的apply方法,返回的是伴生類的對象
例如Array定義了apply方法,因此可以不用new Array,直接使用Array()
//類classAccountprivate(val id:Int,initialBalance:Double){private var balance = initialBalance
}//伴生對象
object Account{
def apply(initialBalance: Double): Account ={newAccount(newUniqueNumber,initialBalance)}private var lastNumber =0private def newUniqueNumber():Int={
lastNumber+=1
lastNumber
}}//使用時不用new了,伴生對象裏定義了apply方法,裏面已經new了
val sam =Account(12)
val bob =Account(400)println(bob.id)
應用程序對象
每個scala程序必須從一個main方法開始,方法的類型爲Array[String] => Unit