Scala 入门笔记

// 偏函数
/**
  * PartialFunction[A, B], A 是参数类型, B 是返回值类型,PartialFunction 常用于模式匹配
  */
object PartialFunctionDemo {

  // String 参数类型,Int返回类型
  def m1: PartialFunction[String, Int] = {
    case "one" => {
      println(" case 1")
      1
    }
    case "two" => {
      println("case 2")
      2
    }
  }

  def m2(num: String) : Int = num match {
    case "one" => 1
    case "two" => 2
    case _ => 0
  }

  def main(args: Array[String]): Unit = {
    println(m1("one"))
    println(m2("two"))
  }
}

  

原文地址:https://www.cnblogs.com/sunnystone85/p/11363681.html