我正在尝试使用IntelliJ在Scala中复制特征的子类型。我调试下面的代码会引发一些错误。我在这里做错了
import scala.collection.mutable.ArrayBuffer
trait Pet {
val name: String
}
class Cat(val name: String) extends Pet
class Dog(val name: String) extends Pet
val dog = new Dog("Harry")
val cat = new Cat("Sally")
object Animals {
def main(args: Array[String]): Unit = {
val animals = ArrayBuffer.empty[Pet]
animals.append(dog)
animals.append(cat)
animals.foreach(pet => println(pet.name)
}
}
我遇到了以下错误
错误:(10,1)预期的类或对象定义
val dogs = new Dogs(“ Harry”)
错误:(11,1)预期的类或对象定义
val cats = new Cats(“ Sally”)