类更像蓝图,而对象是静态的(即已经实例化):
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") // 目的 val dog = new Dog("Barky") // 类