物件

而類更像藍圖,物件是靜態的(即已經例項化):

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