例如,这个:
val queue = new BasicIntQueue with Doubling with Incrementing with Filtering
queue.put(1)
println(queue.get())
将打印:
Filtering
Incrementing
Doubling
put
4
至于我,如果按照我编写操作的顺序从左到右执行,它会更具可读性。
例如,这个:
val queue = new BasicIntQueue with Doubling with Incrementing with Filtering
queue.put(1)
println(queue.get())
将打印:
Filtering
Incrementing
Doubling
put
4
至于我,如果按照我编写操作的顺序从左到右执行,它会更具可读性。
因为它遵循与继承相同的模式。想象一下,你有这样的事情:
class BasicIntQueue
class Doubling extends BasicIntQueue
class Incrementing extends Doubling
class Filtering extends Incrementing
val queue = new Filtering
您将得到与您所看到的相同的结果:Filtering
首先执行,然后传递给Incrementing
,然后Doubling
最后是BasicIntQueue
。
Don't think 'executing', because it's not... think 'layering': if you add another with XYZ
on the right, you've added another layer on top.
事实更复杂,请参阅语言参考“5.1.2 类线性化”(第 56 页),或Jim McBeath的解释
解决钻石多重继承问题