dvm*_*lls 5 scala inversion-of-control
对不起,如果这是一个愚蠢的标题,我不知道如何清楚地表达这一点
说我有一个记录特征:
trait Logging {
def log(s:String)
}
Run Code Online (Sandbox Code Playgroud)
然后一些实施
trait PrintlnLog extends Logging {
def log(s:String) { println(s) }
}
Run Code Online (Sandbox Code Playgroud)
我这样使用
class SomeProcess { this:Logging =>
def doSomeJunk() {
log("starting junk")
...
log("junk finished")
}
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样使用这个类
val p = new SomeProcess () with PrintLog
p.doSomeJunk()
Run Code Online (Sandbox Code Playgroud)
现在怎么样,如果我有这个
class SubProcess { this:Logging =>
def doSubJunk() {
log("starting sub junk")
...
log("finished sub junk")
}
}
class ComplexProcess { this:Logging =>
def doMoreJunk() {
log("starting more junk")
val s = new SubProcess with // ??? <-- help!
s.doSubJunk()
log("finished more junk")
}
}
Run Code Online (Sandbox Code Playgroud)
在ComplexProcess中,我想在混合到ComplexProcess中的相同日志记录特征中实例化一个SubProcess混合,但ComplexProcess不知道它是什么.有没有办法获得它的参考?
你不能这样做。在这种情况下,您可能会这样做:
trait WithSubProcess {
def s: SubProcess
}
class ComplexProcess { this: Logging with WithSubProcess ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |