以下代码的输出让我感到困惑。
代码
object Test1 {
trait A {
val s: String = "A"
println(s"s = $s")
}
abstract class B extends A with DelayedInit {
override def delayedInit(body: => Unit) = {
println("B " + "1" * 80)
body
println("B " + "2" * 80)
}
}
abstract class C extends A with DelayedInit {
override val s: String = "C"
override def delayedInit(body: => Unit) = {
println("C " + "1" * 80)
body
println("C " + "2" * 80)
}
}
def main {
println("-- new B begin")
new B {
println("new B context")
}
println("-- new B end")
println("-- new C begin")
new C {
println("new C context")
}
println("-- new C end")
}
}
object Test2 {
trait A {
def s: String = "A"
println(s"s = $s")
}
abstract class B extends A with DelayedInit {
override def delayedInit(body: => Unit) = {
println("B " + "1" * 80)
body
println("B " + "2" * 80)
}
}
abstract class C extends A with DelayedInit {
override def s: String = "C"
override def delayedInit(body: => Unit) = {
println("C " + "1" * 80)
body
println("C " + "2" * 80)
}
}
def main {
println("-- new B begin")
new B {
println("new B context")
}
println("-- new B end")
println("-- new C begin")
new C {
println("new C context")
}
println("-- new C end")
}
}
println("Test1")
Test1.main
println("Test2")
Test2.main
输出
Test1
-- new B begin
s = A
B 11111111111111111111111111111111111111111111111111111111111111111111111111111111
new B context
B 22222222222222222222222222222222222222222222222222222222222222222222222222222222
-- new B end
-- new C begin
s = null
C 11111111111111111111111111111111111111111111111111111111111111111111111111111111
C 22222222222222222222222222222222222222222222222222222222222222222222222222222222
C 11111111111111111111111111111111111111111111111111111111111111111111111111111111
new C context
C 22222222222222222222222222222222222222222222222222222222222222222222222222222222
-- new C end
Test2
-- new B begin
s = A
B 11111111111111111111111111111111111111111111111111111111111111111111111111111111
new B context
B 22222222222222222222222222222222222222222222222222222222222222222222222222222222
-- new B end
-- new C begin
s = C
C 11111111111111111111111111111111111111111111111111111111111111111111111111111111
new C context
C 22222222222222222222222222222222222222222222222222222222222222222222222222222222
-- new C end
尝试了 scala 2.10.2 和 2.11.0-M3。
在我的理解中,Test1 应该与 Test2 相同。这里有两个问题:
- 为什么DelayedInit 在Test1.C 中被调用了两次?没有其他类继承类层次结构中的特征
- 为什么
s == null
在 Test1.C 中?