Scala Design Patterns.
上QQ阅读APP看书,第一时间看更新

Initialization

Now that we know what happens during linearization, we will understand how instances are being created. The rule is that the constructor code is executed in a reverse order compared to the linearization order. This means that, going from right to left, first the Any and AnyRef constructors will be invoked and then the actual class constructor will be called. Also, the superclass constructor will be called before the actual class or any of its mixins because, as we have already mentioned previously, it is added as a suffix.

Keeping in mind that we traverse the linearization from right to left also means that after the superclass constructor is called, the mixin trait constructors will be called. Here, they will be called in the order in which they appear in the original class definition (because of the right to left direction and the fact that their order is reversed when the linearization is created).