模式匹配

模式匹配之字符串

package day03.matchdemo

import scala.util.Random

/**
  * 匹配字符串
  */
object MatchStr {
  def main(args: Array[String]): Unit = {

    val arr = Array("xuezhiqian","huachenyu","yangmi","feiyuqing")
    val name = arr(Random.nextInt(arr.length))

    println(name)

    name match {
      case "xuezhiqian" => println("薛之谦")
      case "huachenyu" => println("华晨宇")
      case "yangmi" => println("杨幂")
      case "feiyuqing" => println("费玉清")
      case _ => println("Nothing")
    }
  }
}

模式匹配之匹配类型

package day03.matchdemo

import scala.util.Random

object MatchType {

  def main(args: Array[String]): Unit = {

    val arr = Array("abcde",100,3.14,true,MatchType)

    val element = arr(Random.nextInt(arr.length))

    println(element)

    element match {
      case str: String => println(s"match String: $str")
      case int: Int => println(s"match Int: $int")
      case bool: Boolean => println(s"match Boolean: $bool")
      case matchTest: MatchTest => println(s"match MatchTest: $matchTest")
      case _: Any => println("Not Matched")
    }
  }
}

class MatchTest {

}

模式匹配之匹配数组、元组、集合

package day03.matchdemo

/**
  * 匹配数组、元组、集合
  */
object MatchList {

  def main(args: Array[String]): Unit = {

    // 匹配数组
//    val arr = Array(3,2,5,7)
//
//    arr match {
//      case Array(3,a,b,c) => println(s"case:$a,$b,$c")
//      case Array(_,x,y) => println(s"case:$x,$y")
//      case _ => println("Not Matched")
//    }

    // 匹配元组
//    val tup = (2,3,4)
//
//    tup match {
//      case (3,a,b) => println(s"case: $a,$b")
//      case (_,x,y) => println(s"case: $x,$y")
//      case _ => println("Not Matched")
//    }

    //匹配集合
    val list1 = List(0,1,2,3)

    list1 match {
      case 0 :: Nil => println("case: 0")
//      case 0 :: x :: y :: z :: Nil => println("case: 0")
      //      case List(0,a,b,c) => println("case: 0")
      case a :: b :: c :: d :: Nil => println(s"case2: $a,$b,$c,$d")
      case 0 :: a => println(s"case: $a")
      case _ => println("Not Matched")
    }
  }
}

模式匹配之样例类

package day03.matchdemo

import scala.util.Random

object CaseClassDemo {
  def main(args: Array[String]): Unit = {

    val arr = Array(CheckTimeOutTask,SubmitTask("1000","task-00001"),HeartBeat(3000))

    arr(Random.nextInt(arr.length)) match {
      case CheckTimeOutTask => println("CheckTimeOutTask")
      case SubmitTask(port,task) => println("SubmitTask")
      case HeartBeat(time) => println("HeartBeat")
    }
  }
}

case class HeartBeat(time: Long)
case class SubmitTask(id: String, taskName: String)
case object CheckTimeOutTask

模式匹配之偏函数

package day03.matchdemo

/**
  * PartialFunction[A,B],其中A是参数类型,Bshi返回值类型,PartialFunction(偏函数)常用作输入模式匹配
  */
object PartialFunctionDemo {

  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"))

  }
}

你可能感兴趣的:(模式匹配)