5

例如,这个:

val queue = new BasicIntQueue with Doubling with Incrementing with Filtering
queue.put(1)
println(queue.get())

将打印:

Filtering
Incrementing
Doubling
put
4

至于我,如果按照我编写操作的顺序从左到右执行,它会更具可读性。

4

4 回答 4

7

因为它遵循与继承相同的模式。想象一下,你有这样的事情:

class BasicIntQueue 
class Doubling extends BasicIntQueue
class Incrementing extends Doubling
class Filtering extends Incrementing

val queue = new Filtering

您将得到与您所看到的相同的结果:Filtering首先执行,然后传递给Incrementing,然后Doubling最后是BasicIntQueue

于 2013-07-12T17:48:20.887 回答
6

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.

于 2013-07-12T17:25:06.907 回答
3

事实更复杂,请参阅语言参考“5.1.2 类线性化”(第 56 页),或Jim McBeath的解释

于 2013-07-12T17:50:30.000 回答
0

解决钻石多重继承问题

于 2013-07-12T17:44:17.457 回答