加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 将自我类型注释类传递给子对象

发布时间:2020-12-16 09:56:37 所属栏目:安全 来源:网络整理
导读:对不起,如果这是一个愚蠢的标题,我不知道如何清楚地表达这一点 说我有一个记录特征: trait Logging { def log(s:String)} 然后一些实施 trait PrintlnLog extends Logging { def log(s:String) { println(s) }} 我这样使用 class SomeProcess { this:Loggin
对不起,如果这是一个愚蠢的标题,我不知道如何清楚地表达这一点

说我有一个记录特征:

trait Logging {
    def log(s:String)
}

然后一些实施

trait PrintlnLog extends Logging {
    def log(s:String) { println(s) }
}

我这样使用

class SomeProcess { this:Logging =>
   def doSomeJunk() {
      log("starting junk")
      ...
      log("junk finished")
   }
}

我可以像这样使用这个类

val p = new SomeProcess () with PrintLog
p.doSomeJunk()

现在怎么样,如果我有这个

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")
   }
}

在ComplexProcess中,我想在混合到ComplexProcess中的相同日志记录特征中实例化一个SubProcess混合,但ComplexProcess不知道它是什么.有没有办法获得它的参考?

解决方法

你不能这样做.在这种情况下,你可能会做这样的事情:

trait WithSubProcess {
  def s: SubProcess
}

class ComplexProcess { this: Logging with WithSubProcess ... }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读