欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Scala]学习笔记四——模式匹配 match

程序员文章站 2022-03-04 14:01:51
...

一、最基本的模式匹配

例子:

//格式为:
//变量 match{
//			case value1=>代码
//			case value2=>代码
//			……
//			case _=>代码
//}

  def judgeGrade(grade:String): Unit ={
    grade match{
      case "A"=>println("Excellent")
      case "B"=>println("Good")
      case "C"=>println("Just so so")
      case _=>println("You need work hard")
    }
  }
  judgeGrade("A")
  judgeGrade("D")

二、加条件进行模式匹配,可以双重过滤

//在最后通配符的case中添加if语句,来双重过滤

  def judgeGrade(name:String,grade:String): Unit ={
    grade match{
      case "A"=>println("Excellent")
      case "B"=>println("Good")
      case "C"=>println("Just so so")
      case _ if(name=="Nina")=>println("You are absent the exam")
      case _=>println("You need work hard")
    }
  }
  judgeGrade("Lily","A")
  judgeGrade("Nina","D")
  judgeGrade("Sam","D")

三、Array匹配模式

def greeting(array:Array[String]): Unit = {
    //对数组中的内容进行匹配
    array match  {
      case Array("Zhangsan") => println("Hi, " + "Zhangsan")    //数组中仅有这个元素
      case Array(x, y) => println("Hi, " + x + " and " + y)     //数组中有两个元素
      case Array("Zhangsan", _*) => println("Hi, " + "Zhangsan" + " and other friends")       //数组中有多个元素,但开头的元素为zhangsan
      case _ => println("Hi, everybody")    //数组中的内容为其他情况
    }
  }
  greeting(Array("Zhangsan"))
  greeting(Array("Lisi","Nina"))
  greeting(Array("Zhangsan","Lisi","Wangwu"))
  greeting(Array("Lisi","Wangwu","Zhangsan"))

结果:
Hi, Zhangsan
Hi, Lisi and Nina
Hi, Zhangsan and other friends
Hi, everybody

四、List匹配模式

  def greeting(list:List[String]): Unit = {
    //对数组中的内容进行匹配
    list match  {
      case "Zhangsan"::Nil => println("Hi, " + "Zhangsan")    //数组中仅有这个元素
      case x::y::Nil => println("Hi, " + x + " and " + y)     //数组中有两个元素
      case "Zhangsan"::tail => println("Hi, " + "Zhangsan" + " and other friends")       //数组中有多个元素,但开头的元素为zhangsan
      case _ => println("Hi, everybody")    //数组中的内容为其他情况
    }
  }
  greeting(List("Zhangsan"))
  greeting(List("Lisi","Nina"))
  greeting(List("Zhangsan","Lisi","Wangwu"))
  greeting(List("Lisi","Wangwu","Zhangsan")

结果:
Hi, Zhangsan
Hi, Lisi and Nina
Hi, Zhangsan and other friends
Hi, everybody

五、类型匹配

//类型匹配
  def typeMatch(word:Any){
    word match {
      case n:Int=>println("Int")
      case s:String=>println("String")
      case m:Map[_,_]=>m.foreach(println)
      case _=>println("Other type")
    }
  }

  typeMatch(1)
  typeMatch("1")
  typeMatch(1f)
  typeMatch(Map("name"->"Nina"))

结果:
Int
String
Other type
(name,Nina)

六、异常处理

try{
    val i=10/0
    println(i)
  }catch{
    case e:ArithmeticException=>println("除数不能为0")
    case e:Exception=>println("出错了")
  }finally {
    //一定会执行的语句
    //若需要读取文件,这里则需要关闭一些文件流
  }

结果:
除数不能为0

七、case class模式匹配

def matchOffer(person: Person): Unit ={
    person match{
      case CTO(name,floor)=>println("CTO "+name +" is on "+ floor)
      case Employee(name,floor)=>println("Employee "+name +" is on "+ floor)
      case _=>println("Other")
    }
  }

  matchOffer(CTO("Nina","22"))
  matchOffer(Employee("Nina","2"))
  matchOffer(Other("Nina"))

//定义了几个类
class Person
case class CTO(name:String,floor:String) extends Person
case class Employee(name:String,floor:String) extends Person
case class Other(name:String) extends Person

结果:
CTO Nina is on 22
Employee Nina is on 2
Other

八、Some None模式匹配

val map=Map("Nina"->"A","Lily"->"B")
  //Some和None的模式匹配
  def gradeMatch(name:String): Unit = {
    val grade = map.get(name)
    grade match {
      case Some(grade) => println(name + " your grade is " + grade)
      case None => println("Sorry...")
    }
  }

    gradeMatch("Nina")
    gradeMatch("Lily")
    gradeMatch("Zhangsan")

结果:
Nina your grade is A
Lily your grade is B
Sorry...
相关标签: Scala