scala模式匹配-基础使用1.3,1.4

44 阅读3分钟

高阶匹配之匹配元素特征

模式匹配还可以根据数组元素的个数,元素的特征来匹配。

例如如下的格式:

数组 match {

case Array(1,x,y) => 匹配到数组,长度为3,且第一个元素为1

case Array(0) =>匹配到数组:长度为1,且只有一个元素0
case Array(1,_*) =>匹配到数组:长度为n,第一个元素为1

    case _ => 其他

}

package matchcase

object case02 {
    /*
      match case 高阶匹配
      1. 匹配元组不同的元素的数量
      2. 匹配数组特殊值
      3. 匹配变量的类型
      a:Int = 1;
     */
    def matchType(value:Any):Unit = {
      value match {
        case x:Int => println(s"$x: 是Int")
        case x:Double => println(s"$x: 是Double")
        case _ => println("未知")
      }
    }

    def main(args: Array[String]): Unit = {
      matchType(1)
      matchType(1.1)
    }
}

高阶匹配之匹配案例类

模式匹配还根据案例类的不同,匹配不同的案例类和其中的字段信息。例如格式

对象 match {

case 样例类类型1(字段1,字段2)=> 表达式

case 样例类类型2(字段1,字段2,字段3)=> 表达式

    case _ => 其他

}

作者:花开花富贵
链接:juejin.cn/post/758630…
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

package matchcase

object case02 {
    /*
      match case 高阶匹配
      1. 匹配元组不同的元素的数量
      2. 匹配数组特殊值
      3. 匹配变量的类型
      a:Int = 1;
     */
    def matchType(value:Any):Unit = {
      value match {
        case x:Int => println(s"$x: 是Int")
        case x:Double => println(s"$x: 是Double")
        case _ => println("未知")
      }
    }

    def main(args: Array[String]): Unit = {
      matchType(1)
      matchType(1.1)
    }
}

当传入Circle类型时,匹配case Circle(r)并按照圆的面积公式计算;当传入Rectangle类型时,匹配case Rectangle(w, h)并按照矩形面积公式计算。 3,

object case03 {
  /*
    match case 高阶匹配
    1. 匹配元组不同的元素的数量
    2. 匹配数组特殊值
    3. 匹配变量的类型
    4. 匹配样例类(属性值)
   */
  // 1. 圆形类
  case class Circle(radius: Double) {}
  // 2. 矩形类
  case class Rectangle(width: Double, height: Double) {}

  // 封装一个求面积的方法
  def getArea(shape: Any): Double = {
    shape match {
      case Circle(radius) => math.Pi * radius * radius
      case Rectangle(width, height) => width * height
      case _ => 0.0
    }
  }

  def main(args: Array[String]): Unit = {
    // 1. 实例化圆形对象
    val circle = Circle(2.0)
    // 2. 实例化矩形对象
    val rectangle = Rectangle(2.0, 3.0)

    println(getArea(circle))
    println(getArea(rectangle))
  }
}

4,

package matchcase

object case04 {
    // 1. for 循环中,做匹配。
    def main(args: Array[String]): Unit = {
      val m1 = Map(
        "马云 - 阿里巴巴" -> 1964,
        "张一鸣 - 字节跳动" -> 1983,
        "刘强东 - 京东" -> 1974,
        "程维 - 滴滴" -> 1983
      )
      // 请在这个map中,找出所有1983年出生的企业家
      for((key,1983) <- m1){
        println(key)
      }

      for((key,value) <- m1){
        if(value == 1983)
          println(key)
      }
      // m1.foreach({
      //   case (key,value)=>{
      //     if(value == 1983)
      //     println(key)
      //   }
      // })
    }
  }

5

package matchcase

object case05 {
    def main(args: Array[String]): Unit = {
      // 定义一个数组,其中有4个元素
      val arr = Array(11, 22, 33, 4,5,6)

      // 需求:补充定义三个变量:x,y,z分别保持数组arr中的第1,2,3个元素的值
      // var x = arr(0)
      // var y = arr(1)
      // var z = arr(2)
      // println("x=" + x + ",y=" + y + ",z=" + z)

      // 优化:使用match case表达式,完成上述需求
      val Array(x,y,z,_*) = arr

      println("x=" + x + ",y=" + y + ",z=" + z)
    }
  }

8.png