我有以下代码:
trait SuperX {
val v: Int
}
class SubY(val v: Int, var z: SuperX) extends SuperX
class SubZ(val v: Int) extends SuperX
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这是不可能的
var test: SuperX = new SubY(1, new SubZ(-1))
println(test.z.v)
Run Code Online (Sandbox Code Playgroud)
如果我把它写成
var test = new SubY(1, new SubZ(-1))
Run Code Online (Sandbox Code Playgroud)
然后我无法做到
test = test.z
Run Code Online (Sandbox Code Playgroud)
我是Scala的新手,所以有些事情让人很困惑.我知道在Java中可以使用接口而不是特性.
谢谢你的帮助.