scala中的模式匹配java中的switchCase升級版


case class SendHeartBeat(id: String, time: Long)
case object CheckTimeOutWorker

/**
  * 模式匹配 match case
  * 一旦一個case 匹配上了,就不會再往下匹配了
  */
object ScalaMatchCse {

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

        // 匹配字符串內容
        def contentMatch(str: String) = str match {
            case "hello" => println("hello")
            case "Dog" => println("Dog")
            case "1" => println("1")
            case "1" => println("2")
            case _ => println("匹配不上") // _ 任意內容
        }
        println("----------匹配字符串內容--------")
        contentMatch("hello")
        contentMatch("Dog")
        contentMatch("1")
        contentMatch("fuck")


        // 匹配數據類型
        println("----------匹配數據類型--------")
        def typeMatch(tp: Any) = tp match {
            case x: Int => println(s"Int $x")
            case y: Long => println(s"Long $y")
            case b: Boolean => println(s"boolean $b")
            case _ => println("匹配不上")
        }
        typeMatch(1)
        typeMatch(10L)
        typeMatch(true)
        typeMatch("Scala")


        // 匹配Array
        println("----------匹配Array--------")
        def arrayMatch(arr: Any) = arr match {
            case Array(0) => println("只有一個0元素的數組")
            case Array(0, _) => println("以0開頭的,擁有2個元素的數組")
            case Array(1, _, 3) => println("已1開頭,3結尾,中間為任意元素的三個元素的數組")
            case Array(8, _*) => println("已8開頭,N個元素的數組") // _*標識0個或者多個任意類型的數據
        }
        arrayMatch(Array(0))
        arrayMatch(Array(0, "1"))
        arrayMatch(Array(1, true, 3))
        arrayMatch(Array(8,9,10,100,666))

        // 匹配List
        println("----------匹配List--------")
        def listMatch(list: Any) = list match {
            case 0 :: Nil => println("只有一個0元素的List")
            case 7 :: 9 :: Nil => println("只有7和9元素的List")
            case x :: y :: z :: Nil => println("只有三個元素的List")
            case m :: n if n.length > 0 => println("------") // 擁有head,和 tail的數組   if n.length > 0  守衛
            case _ => println("匹配不上")
        }
        listMatch(List(0))
        listMatch(List(7,9))
        listMatch(List(8,9, 666))
        listMatch(List(666))

        // 匹配元組
        println("----------匹配元組--------")
        def tupleMatch(tuple: Any) = tuple match {
            case (0, _) => println("元組的第一個元素為0, 第二個元素為任意類型的數據,且只有2個元素")
            case (x, m, k) => println("擁有三個元素的元組")
            case (_, "AK47") => println("AK47")

        }
        tupleMatch((0, 1))
        tupleMatch(("2", 1, true))
        tupleMatch((ScalaMatchCse, "AK47"))

        // 匹配對象
        println("----------匹配對象--------")
        def objMatch(obj: Any) = obj match {
            case SendHeartBeat(x, y) => println(s"$x $y")
            case CheckTimeOutWorker => println("CheckTimeOutWorker")
            case "registerWorker" => println("registerWorker")
        }
        objMatch(SendHeartBeat("appid0000001", System.currentTimeMillis()))
        objMatch(SendHeartBeat("xx", 100L))
        objMatch(CheckTimeOutWorker)
        objMatch("registerWorker")

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM