scala 案例类默认应用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34533539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case Class default apply method
提问by ZAHEER AHMED
Assuming we have the following case class:
假设我们有以下案例类:
case class CasePerson(firstName: String)
And we also define a companion object for it:
我们还为它定义了一个伴生对象:
object CasePerson {
def apply() = new CasePerson( "XYZ" )
}
Notice that in the example above I explicitly defined a companion object with an applymethod, without defining the the default apply method:
请注意,在上面的示例中,我使用apply方法显式定义了一个伴随对象,而没有定义默认的 apply 方法:
// This "default" apply has the same argument as the primary constructor of the case class
def apply(firstName : String) = new CasePerson(firstName)
Q:So where does Scala gets this "default" apply? I explicitly defined the companion object here without the default apply and the compiler still knows how to execute this:
问:那么 Scala 在哪里应用这个“默认”?我在这里明确定义了伴随对象,没有默认应用,编译器仍然知道如何执行:
val casePerson = CasePerson("PQR")
How does this work?
这是如何运作的?
回答by marios
Case classes are implicitly accompanied with a companion object with an apply()that has the same arguments as the primary constructor of the case class.
案例类隐含地伴随着一个伴随对象,该对象具有与案例类apply()的主要构造函数相同的参数。
That is:
那是:
case class CasePerson(firstName: String)
Will be accompanied by
将伴随
object CasePerson{
def apply(firstName: String) = new CasePerson(firstName)
}
Now if you also explicitly define a companion object, you can think of it as appending to the implicitly defined one.
现在,如果您还显式定义了一个伴生对象,您可以将其视为附加到隐式定义的对象。
For instance, in your example you added one new applyto the companion object:
例如,在您的示例中,您向apply伴随对象添加了一个新对象:
object CasePerson{
def apply() = new CasePerson("XYZ")
}
This statement, you can think of it as if it is creating an combined companion object:
这个语句,你可以把它想成是在创建一个组合的伴生对象:
object CasePerson{
def apply() = new CasePerson("XYZ") // the one manually added
def apply(firstName: String) = new CasePerson(firstName) // this one is automatically added
}
Now, if you decide to add your own version of the applythat has the same arguments as the primary constructor, then this will overshadow the default behavior of the case class.
现在,如果您决定添加apply与主构造函数具有相同参数的自己版本的,那么这将掩盖案例类的默认行为。
object CasePerson{
def apply() = new CasePerson("XYZ")
def apply(s: String) = Seq(new CasePerson(s), new CasePerson(s)) // will replace the default factory for the case class
}
Now, if you call CasePerson("hi")it will instead generate:
现在,如果你调用CasePerson("hi")它会生成:
List(CasePerson("hi"), CasePerson("hi"))
回答by Lodewijk Bogaards
Scala case classes are syntactic sugar. When you create a case class the Scala compiler will create a companion object with an applyand an unapplymethod for you, which you can then use as if it simply exists. Hereis a link to more in depth information on case classes.
Scala case 类是语法糖。当你创建一个 case 类时,Scala 编译器会为你创建一个带有apply和unapply方法的伴随对象,然后你可以使用它,就好像它只是存在一样。这是有关案例类的更深入信息的链接。
回答by Mehmet Akif Tütüncü
You may also give your firstNameclass parameter a default value. So you can create an instance with passing no parameter.
你也可以给你的firstName类参数一个默认值。所以你可以创建一个不传递参数的实例。
Example:
例子:
case class CasePerson(firstName : String = "XYZ")
val person = CasePerson()

