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 = ""
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
