特征中的基类构造函数参数
我有一个带有一些构造函数参数的基类:
abstract class HugeClass(implicit context: ContextClass) {
...
}
因为该类变得越来越大,并且只有一些子类需要一些特殊行为,所以我想将其重构为一个特征。但是,我仍然需要访问特征中的context
。我尝试了这个:
trait SomeTrait extends HugeClass {
def myMethod = {
context.method
}
}
但是 scala 编译器说:未找到:值上下文。我该如何解决这个问题?
I have a base class with some constructor parameter:
abstract class HugeClass(implicit context: ContextClass) {
...
}
Because the class gets bigger and bigger and only some subclasses need some special behaviour, I want to refactor it into a trait. However, I still need access to context
in the trait. I tried this:
trait SomeTrait extends HugeClass {
def myMethod = {
context.method
}
}
But the scala compiler says: not found: value context. How can I solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参数
context
变成了私有字段。参数或成员是隐式的这一事实并不意味着它是公共的 - 它仅在其可见的类 (HugeClass
) 中是隐式的。将
context
转换为val
:就可以了。
Parameter
context
is turned into a private field. The fact that a parameter or a member is implicit does not mean it is public - it is implicit only within the class it is visible in (HugeClass
).Turn
context
into aval
:and it will work.