scala – 强制特性实现方法
发布时间:2020-12-16 18:33:18 所属栏目:安全 来源:网络整理
导读:我有一个特征I(中间人),一个混合在特质中的M级(混合器)和一个特征S(特定的). class M extends Something with S { def baz() = foo()}trait I { def foo(): { ...; bar(); ... } def bar()}trait S extends I { def bar() = 42} 我充当M和S之间的中间层,提供
我有一个特征I(中间人),一个混合在特质中的M级(混合器)和一个特征S(特定的).
class M extends Something with S { def baz() = foo() } trait I { def foo(): { ...; bar(); ... } def bar() } trait S extends I { def bar() = 42 } 我充当M和S之间的中间层,提供通用接口. 我在I中实现了一个调用方法栏的方法foo(在I中没有实现但在那里定义).我想要实现的是所有延伸的特性我必须实现bar,这样会抛出编译时错误,因为bar没有实现: trait Z extends I 这可能在Scala中吗? P.S.:我知道Force Scala trait to implement a certain method的答案,但我不想要那种明确的耦合. 解决方法
我考虑过结构子类型:
trait I[T<: { def:foo:Unit}] …. 那会对你有用吗? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |