1 object MixInDemo02 {
2 def main(args: Array[String]): Unit = {
3 val mysql4 = new MySQL4 with File4 with DB4
4 mysql4.insert(100)
5
6 // 执行结果如下
7 // Operate4...
8 // Data4
9 // File4
10 // DB4
11 // 向数据库
12 // 向文件
13 // 插入数据 = 100
14 }
15 }
16
17 trait Operate4 {
18 println("Operate4...")
19 def insert(id : Int)
20 }
21
22 trait Data4 extends Operate4 {
23 println("Data4")
24 override def insert(id : Int): Unit = {
25 println("插入数据 = " + id)
26 }
27 }
28
29 trait DB4 extends Data4 {
30 println("DB4")
31 override def insert(id : Int): Unit = {
32 println("向数据库")
33 super.insert(id)
34 }
35 }
36
37 trait File4 extends Data4 {
38 println("File4")
39 override def insert(id : Int): Unit = {
40 println("向文件")
41 super.insert(id)
42 }}
43 class MySQL4 {}
多特质叠加的时候,构造对象是从左往右开始。
多特质叠加的时候,执行叠加对象里的的方法是从右往左开始。
原文:https://www.cnblogs.com/kyle-blog/p/13583028.html