[英]What does “override implicit” mean?
I am looking at a piece of code that says 我正在看一段代码
case class MyClass(override implicit val x : SomeClass) extends SomeOtherClass(...) {
...
}
What does override implicit
mean in this context, and what can I do if I want to produce an instance of MyClass
with explicit parameters? 在这个上下文中override implicit
意味着什么,如果我想用显式参数生成MyClass
实例,我该怎么办?
This means that SomeOtherClass
has a field x
of type SomeClass
that will be overriden by the x
you pass in the constructor of MyClass
. 这意味着SomeOtherClass
有一个SomeClass
类型的字段x
,它将被你在MyClass
的构造函数中传递的x
覆盖。
The implicit
will make the x
argument for my class implicit
and allow the following code: implicit
将使我的类的x
参数implicit
并允许以下代码:
implicit val someInt = 5
val a = new MyClass
val b = new MyClass()
val c = MyClass() // as it it a case class
If you want to produce an instance of MyClass
with explicit parameters, you can pass them explicitly like this: 如果要使用显式参数生成MyClass
实例,可以像这样显式传递它们:
val a = new MyClass()(42)
(Examples assume that SomeClass
is an Int
, for simplicity) (为简单起见,示例假设SomeClass
是Int
)
To clarify: the implicit
and override
keywords here are unrelated. 澄清一下:这里的implicit
和override
关键字是不相关的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.