Scala多重继承及AOP

Posted yszd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala多重继承及AOP相关的知识,希望对你有一定的参考价值。

 1 package traitandclass
 2 
 3 /**
 4   * Created by zhen on 2018/8/23.
 5   */
 6 class Human {
 7   println("Human")
 8 }
 9 trait Teacher extends Human{
10   println("Teacher")
11   def teach
12 }
13 trait Player extends Human{
14   println("Player")
15   def play = {println("I‘m playing games!")}
16 }
17 // 继承Human类,混入Teacher和Player特质
18 class GameTeacher extends Human with Teacher with Player{
19   override def teach = {println("I‘m teach students how to play games!")}
20 }
21 
22 //AOP
23 trait Action{
24   def doAction
25 }
26 trait BeforeAfter extends Action{
27   abstract override def doAction{
28     println("Initial...")
29     super.doAction
30     println("Destroyed")
31   }
32 }
33 class Work extends Action{
34   override def doAction = println("Working...")
35 }
36 object Test{
37   def main(args: Array[String]) {
38     val gameTeacher = new GameTeacher
39     gameTeacher.play
40     gameTeacher.teach
41     val overrideTeacher = new Human with Teacher with Player{
42       override def teach = {println("I‘m teach students how to play games!")}
43     }
44     overrideTeacher.play
45     overrideTeacher.teach
46     //测试AOP,混入BeforeAfter
47     val work = new Work with BeforeAfter
48     work.doAction
49   }
50 }

结果:

技术分享图片

 

以上是关于Scala多重继承及AOP的主要内容,如果未能解决你的问题,请参考以下文章

多重继承及虚继承中对象内存的分布

java 代码片段

c++多重继承及优缺点

c++多重继承及优缺点

Scala入门2(特质与叠加在一起的特质)

scala ide是干啥用的