0
object sandbox {
class Numbers {
def price() : List[Int] = List(1,3,5,7)
def printIt(): Unit = {
price.foreach(x => print(x+ " "))
}
}
trait DoubleIt extends Numbers {
override def price() : List[Int] ={
println("doubling")
super.price.map(x => x*2)
}
}
trait AddIt extends Numbers {
override def price() : List[Int] = {
println("adding")
super.price.map(x => x+2)
}
}
def main(args :Array[String]): Unit = {
val obj = new Numbers with DoubleIt with AddIt
obj.printIt()
}
}
//output :
adding
doubling
4 8 12 16
在上面的代碼,從AddIt
性狀price()
方法執行第一(從打印語句)。但不應值是6 10 14 18
?爲什麼在添加之前數值會增加一倍?Scala的性狀密新訂購