目录
基本数据类型模式匹配
Array模式匹配
List模式匹配
Scala异常处理
case class模式匹配
Some&None模式匹配
Java:对一个值进行条件判断,返回针对不同的条件进行不同的处理
Scala:
变量 match{
case value1 => 代码1
case value2 => 代码2
...
case _ => 代码N
}
最基本的模式匹配
package com.imooc.scala.course06
import scala.util.Random
object MatchApp extends App {
val names = Array("Akiho Yoshizawa", "YuiHatano", "Aoi Sola")
val name = names(Random.nextInt(names.length))
name match {
case "Akiho Yoshizawa" => println("吉老师")
case "YuiHatano" => println("波老师")
case _ => println("知不知道你们在说什么")
}
def judgeGrade(grade: String): Unit = {
grade match {
case "A" => println("666")
case "B" => println("很好")
case "C" => println("继续加油")
case _ => println("你要努力学习")
}
}
judgeGrade("A")
judgeGrade("C")
judgeGrade("D")
}
双重过滤
package com.imooc.scala.course06
import scala.util.Random
object MatchApp extends App {
def judgeGrade(name: String, grade: String): Unit = {
grade match {
case "A" => println("666")
case "B" => println("很好")
case "C" => println("继续加油")
case _ if (name == "list") => println(name + "你要努力学习")
case _ => println("你要努力学习")
}
}
judgeGrade("zhangsan", "D")
judgeGrade("list", "A")
judgeGrade("list", "D") //双重过滤
}
Array模式匹配
package com.imooc.scala.course06
import scala.util.Random
object MatchApp extends App {
def greeting(array: Array[String]): Unit = {
array match {
case Array("zhangsan") => println("HI zhangsan")
case Array(x, y) => println("HI " + x + " " + y)
case Array("zhangsan", _*) => println("HI zhangsan and other friends...")
case _ => println("HI everybody...")
}
}
greeting(Array("zhangsan"))
greeting(Array("list", "wangwu"))
greeting(Array("zhangsan", "list", "wangwu"))
greeting(Array("list", "wangwu", "lubenwei"))
}
List模式匹配
package com.imooc.scala.course06
import scala.util.Random
object MatchApp extends App {
def greeting(list: List[String]): Unit = {
list match {
case "zhangsan" :: Nil => println("HI zhangsan")
case x :: y :: Nil => println("HI " + x + " " + y)
case "zhangsan" :: tail => println("HI zhangsan and other friends...")
case _ => println("HI everybody...")
}
}
greeting(List("zhangsan","lisi"))
greeting(List("lubenwei","zhangsan","lisi"))
}
判断类型
package com.imooc.scala.course06
import scala.util.Random
object MatchApp extends App {
def matchType(obj: Any): Unit = {
obj match {
case x: Int => println("Int")
case s: String => println("String")
case m: Map[_, _] => m.foreach(println)
case _ => println("other type")
}
}
matchType(1) //Int
matchType("1") //String
matchType(1l) //other typ
matchType(1f) //other typ
matchType(Map("name" -> "PK")) //(name,PK)
}
Scala异常处理
package com.imooc.scala.course06
object ExceptionApp extends App {
try {
val i = 10 / 0
println(i)
} catch {
case e: ArithmeticException => println("除数不能为0")
case e: Exception => println(e.getMessage)
} finally {
// 释放资源,一定能执行
}
// IO 当操作文件时一定要关闭
val file = "test.txt"
try {
// open file
} catch {
case e: ArithmeticException => println("除数不能为0")
case e: Exception => println(e.getMessage)
} finally {
// 释放资源,一定能执行:close file
}
}
case class模式匹配
package com.imooc.scala.course06
object ExceptionApp extends App {
def caseclassMath(person: Person): Unit ={
person match {
case CTO(name, floot) => println("CTO name is"+name+" floot is"+floot)
case Employee(name, floot) =>println("CTO Employee is"+name+" floot is"+floot)
case _ =>println("other")
}
}
class Person
case class CTO(name: String, floot: String) extends Person
case class Employee(name: String, floot: String) extends Person
case class Other(name: String) extends Person
caseclassMath(CTO("PK","22"))
caseclassMath(Employee("zhangsan","2"))
caseclassMath(Other("other"))
}