class Counter {
private var value = 0
def increment(step: Int): Unit = { value += step}
def current(): Int = { value }
}
object MyCounter{
def main(args:Array[String]){
val myCounter = new Counter
myCounter.increment(5)
println(myCounter.current)
}
}
scala 中沒有 static 關(guān)鍵字對于一個class來說,所有的方法和成員變量在實例被 new 出來之前都是無法訪問的因此class文件中的main方法也就沒什么用了,scala object 中所有成員變量和方法默認都是 static 的所以 可以直接訪問main方法。