对象

而类更像蓝图,对象是静态的(即已经实例化):

object Dog {
    def bark: String = "Raf"
}

Dog.bark() // yields "Raf"

它们经常被用作课堂的伴侣,它们允许你写作:

class Dog(val name: String) {

}

object Dog {
    def apply(name: String): Dog = new Dog(name)
}

val dog = Dog("Barky") // Object
val dog = new Dog("Barky") // Class