码迷,mamicode.com
首页 > 其他好文 > 详细

Scala多重继承及AOP

时间:2018-08-23 17:41:47      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:dcl   ack   mes   main   player   ini   for   pre   bsp   

 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

标签:dcl   ack   mes   main   player   ini   for   pre   bsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!