Scala match

case class SendHeartBeat(id: String, time: Long)
case object CheckTimeOutWorker
// 匹配上第一个之后就不再匹配
object ScalaMatchCse {
    def main(args: Array[String]): Unit = {
        // 匹配字符串内容
        def contentMatch(str: String) = str match {
            case "aaa" => println("a")
            case "aaa" => println("b")
            case "aaa" => println("c")
            case "aaa" => println("d")
            case _ => println("没有") // _ 任意内容
        }
        contentMatch("aaa")
        contentMatch("bbb")
        contentMatch("ccc")
        contentMatch("ddd")

        // 匹配数据类型
        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
        def arrayMatch(arr: Any) = arr match {
            case Array(0) => println("只有一个0元素的数组")
            case Array(0, _) => println("以0开头的,第二个元素可以是任意值的数组")
            case Array(1, _, 3) => println("已1开头,3结尾,中间为任意元素的三个元素的数组")
            case Array(1, _*) => println("已1开头,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 1 :: 2 :: Nil => println("只有1和2元素的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(1,2))
        listMatch(List(3,4,446))
        listMatch(List(123))

        // 匹配元组

        def tupleMatch(tuple: Any) = tuple match {
            case (0, _) => println("元组的第一个元素为0, 第二个元素为任意类型的数据")
            case (x, m, k) => println("拥有三个元素的元组")
            case (_, "AK47") => println("AK47")
        }
        tupleMatch((0, 1))
        tupleMatch(("1", 1, true))
        tupleMatch((ScalaMatchCse, "ff12"))
        
        // 匹配对象

        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")
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值