package com.uplooking.bigdata.p4
/**
* scala中的模式匹配
* match case操作
*/
object MatchCaseOps {
def main(args: Array[String]): Unit = {
// switchOps
// ifSafeOps
// arrOps
// caseClassOps
sealedOps
}
sealed abstract class TrafficColor
case class Red(name:String) extends TrafficColor
case class Yellow(name:String) extends TrafficColor
case class Green(name:String) extends TrafficColor
def sealedOps: Unit = {//这种密封类的方式经常被用作模拟枚举
val light:TrafficColor = Red("RED")
light match {
case Red(name) => println("red:" + name)
case Yellow(name) => println("Yellow:" + name)
case Green(name) => println("Green:" + name)
}
}
abstract class Expr
case class Var(name:String) extends Expr
case class Number(number:Int) extends Expr
case class UnOp(operator:String, expr:Expr) extends Expr
case class BinOp(Operator:String, left:Expr, right:Expr) extends Expr
def caseClassOps: Unit = {
// val expr:Expr = Var("jack")
val expr:Expr = Number(5)
expr match {
case Var(name) => println("Var(name)" + name)
//Var(name) = Number(5)//在java或者scala中只能父类引用只想了子类对象
case Number(number) => println("Number" + number)
case UnOp("+", Number(0)) => println("UnOp(\"+\", e)")
case BinOp("-", Number(1), Number(2)) => println("BinOp" + BinOp)
}
}
//匹配一个数组、元组等等
def arrOps: Unit = {
val arr = Array(0, 1)
arr match {
//匹配arr数组是否只包含0元素
case Array(0) => println("0")
//匹配arr数组是否只有两个元素,分配赋值给x和y
case Array(x, y) => println("x=" + x + ", y=" + y)
//匹配arr数组是否以0元素开头
case Array(0, _*) => println(arr)
case _ => println("默认情况")
}
}
//模式匹配中使用if表达式对当前匹配变量仅需获取,判断
def ifSafeOps: Unit = {
var sign = 0
val ch = '1'
sign = ch match {
case '+' => 1
case '-' => -1
//Character.isDigit(ch) 判断当前字符ch是否为一个数字类型的字符
case _ if Character.isDigit(ch) => 3
case _ => 0
}
println("sign = " + sign)
}
//模式匹配用作类似java中的switch case的用法
def switchOps: Unit = {
var sign = 0
val ch = 'a'
/*ch match {
case '+' => sign = 1
case '-' => sign = -1
case _ => sign = 0
}*/
sign = ch match {//match case是一个表达式,表达式就有返回值
case '+' => 1
case '-' => -1
case _ => 0
}
println("sign = " + sign)
}
}
/**
* scala中的模式匹配
* match case操作
*/
object MatchCaseOps {
def main(args: Array[String]): Unit = {
// switchOps
// ifSafeOps
// arrOps
// caseClassOps
sealedOps
}
sealed abstract class TrafficColor
case class Red(name:String) extends TrafficColor
case class Yellow(name:String) extends TrafficColor
case class Green(name:String) extends TrafficColor
def sealedOps: Unit = {//这种密封类的方式经常被用作模拟枚举
val light:TrafficColor = Red("RED")
light match {
case Red(name) => println("red:" + name)
case Yellow(name) => println("Yellow:" + name)
case Green(name) => println("Green:" + name)
}
}
abstract class Expr
case class Var(name:String) extends Expr
case class Number(number:Int) extends Expr
case class UnOp(operator:String, expr:Expr) extends Expr
case class BinOp(Operator:String, left:Expr, right:Expr) extends Expr
def caseClassOps: Unit = {
// val expr:Expr = Var("jack")
val expr:Expr = Number(5)
expr match {
case Var(name) => println("Var(name)" + name)
//Var(name) = Number(5)//在java或者scala中只能父类引用只想了子类对象
case Number(number) => println("Number" + number)
case UnOp("+", Number(0)) => println("UnOp(\"+\", e)")
case BinOp("-", Number(1), Number(2)) => println("BinOp" + BinOp)
}
}
//匹配一个数组、元组等等
def arrOps: Unit = {
val arr = Array(0, 1)
arr match {
//匹配arr数组是否只包含0元素
case Array(0) => println("0")
//匹配arr数组是否只有两个元素,分配赋值给x和y
case Array(x, y) => println("x=" + x + ", y=" + y)
//匹配arr数组是否以0元素开头
case Array(0, _*) => println(arr)
case _ => println("默认情况")
}
}
//模式匹配中使用if表达式对当前匹配变量仅需获取,判断
def ifSafeOps: Unit = {
var sign = 0
val ch = '1'
sign = ch match {
case '+' => 1
case '-' => -1
//Character.isDigit(ch) 判断当前字符ch是否为一个数字类型的字符
case _ if Character.isDigit(ch) => 3
case _ => 0
}
println("sign = " + sign)
}
//模式匹配用作类似java中的switch case的用法
def switchOps: Unit = {
var sign = 0
val ch = 'a'
/*ch match {
case '+' => sign = 1
case '-' => sign = -1
case _ => sign = 0
}*/
sign = ch match {//match case是一个表达式,表达式就有返回值
case '+' => 1
case '-' => -1
case _ => 0
}
println("sign = " + sign)
}
}