通常,在一个类的伴生对象中定义apply方法,在生成这个类的对象时,就省去了new关键字。
class Currency(val value: Double, val unit: String) {
}
object Currency{
def apply(value: Double, unit: String): Currency = new Currency(value, unit)
def unapply(currency: Currency): Option[(Double, String)] = {
if (currency == null){
None
}
else{
Some(currency.value, currency.unit)
}
}
}
在构建对象的时候就可以直接使用val currency = Currency(30.2, "EUR")
这种方式,不用使用new。
def main(args: Array[String]): Unit = {
val currency = Currency(30.2, "EUR")
currency match {
case Currency(amount, "USD") => println("$" + amount)
case _ => println("No match.")
}
}
那么unapply方法就刚好相反,他是接受一个对象,从对象中提取出相应的值。
unapply方法主要用于模式匹配中。
trait Person {
def name: String
}
case class Student(name: String, year: Int) extends Person
case class Teacher(name: String, specialty: String) extends Person
def getPrintableString(p: Person): String = p match {
case Student(name, year) =>
s"$name is a student in Year $year."
case Teacher(name, whatTheyTeach) =>
s"$name teaches $whatTheyTeach."
}
Those patterns work because
Student
andTeacher
are defined as case classes that haveunapply
methods whose type signature conforms to a certain standard. Technically, the specific type of pattern matching shown in these examples is known as a constructor pattern.The Scala standard is that an
unapply
method returns the case class constructor fields in a tuple that’s wrapped in anOption
. The “tuple” part of the solution was shown in the previous lesson.
引用官网原文,上面的模式匹配代码之所以能够工作,正是因为定义了unapply方法。
unapply方法接受对象,返回key-value元组,所以后面能够拿到$name和$year属性了。