设为首页 加入收藏

TOP

12. Scala模式匹配(一)
2019-08-15 00:10:54 】 浏览:101
Tags:12. Scala 模式 匹配

12.1 match 

  12.1.1 基本介绍 

      Scala中的模式匹配类似于Java中的switch语法,但是更加强大

      模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需要匹配时,会从第一个case分支开始,如果匹配成功,那么执行对应的逻辑代码,如果匹配不成功,继续执行下一个分支进行判断。如果所有的case都不匹配,那么会执行case_分支,类似于Java中的default语句

  12.1.2 scala的match的快速入门案例 

object boke_demo01 {

  def main(args: Array[String]): Unit = {
    val oper = '+'
    val n1 = 20
    val n2 = 10
    var res = 0
    //说明
    //1. match (类似java switch) 和  case 是关键字
    //2. 如果匹配成功, 则 执行 => 后面的代码块.
    //3. 匹配的顺序是从上到下,匹配到一个就执行对应的 代码
    //4. => 后面的代码块 不要写 break ,会自动的退出match
    //5. 如果一个都没有匹配到,则执行 case _ 后面的代码块
    oper match {
      case '+' => {
        res = n1 + n2
        println("ok~~")
        println("hello~~")
      }
      case '-' => res = n1 - n2
      case '*' => res = n1 * n2
      case '/' => res = n1 / n2
      case 1 => println("匹配到1")
      case 1.1 => println("匹配1.1")
      case _ => println("oper error")
    }
    println("res=" + res)

  }
}

  12.1.3 match的细节和注意事项  

      1) 如果所有case都不匹配,那么会执行case_分支,类似于Java中的default语句

      2) 如果所有case都不匹配,又没有写case_分支,那么会抛出MatchError

      3) 每个case中,不用break语句,自动中断case

      4) 可以在match中使用其它类型,而不仅仅是字符

      5) => 等价于 java switch的 :

      6) => 后面的代码块到下一个case,是作为一个整体执行,可以使用{}扩起来,也可以不扩

12.2 守卫

  12.2.1 基本介绍 

      如果想要表达匹配某个范围的数据,就需要在模式匹配中增加条件守卫

  12.2.2 应用案例  

object boke_demo01 {

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

    for (ch <- "+-3!") { //是对"+-3!" 遍历
      var sign = 0
      var digit = 0
      ch match {
        case '+' => sign = 1
        case '-' => sign = -1
        // 说明..
        // 如果 case 后有 条件守卫即if ,那么这时的 _ 不是表示默认匹配
        // 表示忽略 传入 的 ch
        case _ if ch.toString.equals("3") => digit = 3
        case _ => sign = 2
      }
      //分析
      // + 1 0
      // - -1 0
      // 3 0 3
      // ! 2 0
      println(ch + " " + sign + " " + digit)
    }

  }
}

12.3 模式中的变量 

  12.3.1 基本介绍  

      如果在case关键字后跟变量名,那么match前表达式的值会赋给那个变量

  12.3.2 应用案例 

object boke_demo01 {

  def main(args: Array[String]): Unit = {
    val ch = 'U'
    ch match {
      case '+' => println("ok~")
      // 下面 case mychar 含义是 mychar = ch
      case mychar => println("ok~" + mychar)
      case _ => println("ok~~")
    }

    val ch1 = '+'
    //match是一个表达式,因此可以有返回值
    //返回值就是匹配到的代码块的最后一句话的值
    val res = ch1 match {
      case '+' => ch1 + " hello "
      case _ => println("ok~~")
    }

    println("res=" + res)
  }
}

12.4 类型匹配 

  12.4.1 基本介绍 

      可以匹配对象的任意类型,这样做避免了使用isInstanceOf和asInstanceOf

  12.4.2 应用案例 

object boke_demo01 {

  def main(args: Array[String]): Unit = {
    val a = 8
    //说明 obj 实例的类型 根据 a 的值来返回
    val obj = if (a == 1) 1
    else if (a == 2) "2"
    else if (a == 3) BigInt(3)
    else if (a == 4) Map("aa" -> 1)
    else if (a == 5) Map(1 -> "aa")
    else if (a == 6) Array(1, 2, 3)
    else if (a == 7) Array("aa", 1)
    else if (a == 8) Array("aa")

    //说明
    //1. 根据  obj 的类型来匹配
    // 返回值
    val result = obj match {

      case a: Int => a
      case b: Map[String, Int] => "对象是一个字符串-数字的Map集合"
      case c: Map[Int, String] => "对象是一个数字-字符串的Map集合"
      case d: Array[String] => d //"对象是一个字符串数组"
      case e: Array[Int] => "对象是一个数字数组"
      case f: BigInt => Int.MaxValue
      case y: Float => println("xx")
      case _ => "啥也不是"
    }

    println(result)

  }
}

  12.4.3 类型匹配注意事项 

      1) Map[String, Int]和Map[Int, String]是两种不同的类型,其

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇11. Scala数据结构(下)-集合操作 下一篇Akka-CQRS(7)- CQRS Reader Act..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目