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 }

结果:

原文地址:https://www.cnblogs.com/yszd/p/9525068.html