Scala multiple inheritance. Mixin order v.s. Super order.

Code is everything:

trait Base1 {
  println("mixing Base1")

  def print() {
    println("Base1")
  }
}

trait A extends Base1 {
  println("mixing A")

  override def print() {
    println("A")
    super.print()
  }
}

trait B extends Base1 {
  println("mixing B")

  override def print() {
    println("B")
    super.print()
  }
}

class Base2 {
  println("mixing Base2")

  def print() {
    println("Base2")
  }
}

/**
  * C extends Base2 with A with B
  * => inheritance linearization
  * step 1: expansion
  * C extends Base2 with Base1 with A with Base1 with B
  * step 2: duplication removal
  * C extends Base2 with Base1 with A with B
  *
  * So the mixin order is:
  * Base2 -> Base1 -> A -> B
  *
  * Super order is:
  * B -> A -> Base1 (because Base1 mixes in after Base2, so it overrides it)
  **/
class C extends Base2 with A with B {
  println("creating C")

  override def print() {
    println("C")
    super.print()
  }
}

object Main extends App {
  (new C).print()
  /*
  Output:

mixing Base2
mixing Base1
mixing A
mixing B
creating C
C
B
A
Base1
    */
}
时间: 2024-08-04 15:24:06

Scala multiple inheritance. Mixin order v.s. Super order.的相关文章

字节序(byte order)和位序(bit order)

字节序(byte order)和位序(bit order) ?在网络编程中经常会提到网络字节序和主机序,也就是说当一个对象由多个字节组成的时候需要注意对象的多个字节在内存中的顺序. ?以前我也基本只了解过字节序,但是有一天当我看到ip.h中对IP头部结构体struct iphdr的定义时,我发现其中竟然对一个字节中的8个比特位也区分了大小端,这时我就迷糊了,不是说大小端只有在多个字节之间才会有区分的吗,为什么这里的定义却对一个字节中的比特位也区分大小端呢? ?下面我们先看一下struct iph

面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】

Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: 1 Shape * p1; //使用指针创建对象的方法 2 p = new Circle (2.0); 3 Shape * p2; 4 p = new Rectangle (3.0, 5.0); 5 6 class Shape { 7 public: 8 virtual double area () = 0; //Pure virtual function 9 } 10 11 //Warning:

Object Oriented Programming python

new concepts of the object oriented programming : class encapsulation inheritance polymorphism the three features of an object are : identity, state and behaviora class is an abstraction which regroup objects who have the same attributes and the same

深入理解JavaScript系列(17):面向对象编程之概论

介绍 在本篇文章,我们考虑在ECMAScript中的面向对象编程的各个方面(虽然以前在许多文章中已经讨论过这个话题).我们将更多地从理论方面看这些问题. 特别是,我们会考虑对象的创建算法,对象(包括基本关系 - 继承)之间的关系是如何,也可以在讨论中使用(我希望将消除之前对于JavaScript中OOP的一些概念歧义). 英文原文:http://dmitrysoshnikov.com/ecmascript/chapter-7-1-oop-general-theory/ 概论.范式与思想 在进行E

Memory Layout for Multiple and Virtual Inheritance

2016/6/7 phc ­­ Memory Layout for Multiple and Virtual Inheritance ­­ Edsko de Vrieshttp://www.phpcompiler.org/articles/virtualinheritance.html 1/10Home | Download phc | Documentation | Developers and Contributors | Mailing List |Memory Layout for Mu

Design Pattern: Not Just Mixin Pattern

Brief 从Mix-In模式到Mixin模式,中文常用翻译为“混入/织入模式”.单纯从名字上看不到多少端倪,而通过采用Mixin模式的jQuery.extend我们是否可以认为Mixin模式就是深拷贝的代名词呢? 本文试图从继承机制入手对Mixin模式进行剖析,若有纰漏请大家指正,谢谢. The Fact of Inheritance     首先让我们一起探讨一下继承机制吧.作为OOP的三个重要特性(Inheritance,Polymorphism,and Encapsulation)之一,

Classical Inheritance in JavaScript

JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript's

Python内置函数(63)——super

英文文档: super([type[, object-or-type]]) Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used b

<Scala><For beginners>

Scala Overview Scala is object-oriented Scala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits. Classes are extended by subclassing and a flexible mixin-b