scala入门初级代码训练-14AOP编程,trait 的混入

原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-14/

There I am,in the world more exciting!

by postbird

www.ptbird.cn


package com.ptbird.scala

/**
* Created by postbird on 2016/5/25.
*/
//AOP编程
trait Action{
def doAction
}
trait TBeforeAfter extends Action{
//这个函数乜有实现完整 必须使用 abstract and override
abstract override def doAction: Unit ={
println("Initialization")
super.doAction
println("Destroyed")
}
}
class Work extends Action {
override def doAction ={
println("Working.........")
}
}

object ClassAOPTest {
def main(args: Array[String]): Unit = {
val w =new Work with TBeforeAfter
w.doAction
// Initialization
// Working.........
// Destroyed

//由于 TBeforeAfter 混入了 w 中,因此会执行TBeforeAfter 的 action
}
}

欢迎评论。
lingkb » scala入门初级代码训练-14AOP编程,trait 的混入

发表评论