Scala中的多特征构造错误

Lal*_*lin 2 scala anonymous-class

有人可以帮我理解这里的错误吗?我想我理解Scala中具有特征的匿名类构造.但是,当我尝试应用多个特征时,我得到一个错误,期待";" 或必要的声明结束.如果我也以这种方式声明一个类(具有需要匿名实现代码行的多个特性),同样的问题似乎也适用吗?Line Test 3在下面失败了.谢谢.

class TestTraits 

trait A {def x:Int}
trait B {def y:Int}



object TestTraits {

  def main(args: Array[String]): Unit = {

     val test1 = new TestTraits with A {def x=22}  //OK

     val test2 = new TestTraits with B {def y=33} //OK

     val test3 = new TestTraits with A {def x=22} with B {def y=33} //Errors: - ';' expected but 'with' 


  }  
}
Run Code Online (Sandbox Code Playgroud)

oxb*_*kes 5

您的语法无效:

val test3 = new TestTraits with A with B {def x=22; def y=33} 
Run Code Online (Sandbox Code Playgroud)

类定义只能有一个主体,而您声明的是一个匿名类.