-
trait Logger {
-
def log (msg : String) }
-
trait Auth {
-
auth : Logger => //self-type限定,使用Auth必须混入logger的实现,达到依赖注入的效果。
-
def act(msg : String) {
-
log(msg) //Logger的方法
-
}
-
}
-
object DI extends Auth with Logger {
-
override def log(msg : String) = println(msg);//具体实现
-
}
-
object Dependency_Injection {
-
-
def main(args: Array[String]) {
-
DI.act("I hope you'll like it")
-
}
-
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28912557/viewspace-1985103/
,如需转载,请注明出处,否则将追究法律责任。