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

scala – 在特征之间共享通用

发布时间:2020-12-16 09:58:35 所属栏目:安全 来源:网络整理
导读:我有两个特点 trait Base[A]trait SometimesUsedWithBase[A] { this: Base[A] =} 然后我在课堂上使用它们 class StringThing extends Base[String] with SometimesUsedWithBase[String] 如果我不必定义CertainUsedWithBase的类型,那将是很好的,而是它以某种
我有两个特点

trait Base[A]

trait SometimesUsedWithBase[A] {
    this: Base[A] =>
}

然后我在课堂上使用它们

class StringThing extends Base[String] with SometimesUsedWithBase[String]

如果我不必定义CertainUsedWithBase的类型,那将是很好的,而是它以某种方式理解它使用Base中定义的类型,使它看起来像:

class StringThing extends Base[String] with SometimesUsedWithBase

这可能吗?

解决方法

你应该可以做这样的事情.

trait Base[A] {
  type BaseType = A
}

trait SometimesUsedWithBase {
  this: Base[_] =>
  def someFunction: BaseType
}

class StringThing extends Base[String] with SometimesUsedWithBase {
  def someFunction: String = ""
}

(编辑:李大同)

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

    推荐文章
      热点阅读