Scala中apply方法與unapply方法

通常,在一個類的伴生對象中定義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 and Teacher are defined as case classes that have unapply 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 an Option. The “tuple” part of the solution was shown in the previous lesson.

引用官網原文,上面的模式匹配代碼之所以能夠工作,正是因爲定義了unapply方法。

unapply方法接受對象,返回key-value元組,所以後面能夠拿到$name和$year屬性了。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章