简体   繁体   English

“覆盖隐含”是什么意思?

[英]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) (为简单起见,示例假设SomeClassInt

To clarify: the implicit and override keywords here are unrelated. 澄清一下:这里的implicitoverride关键字是不相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM