package
com.jn.scala.scalalearn
/**
* Created by admin on 2016/1/10.
*/
object MatchTest {
//模式匹配是数据结构上的概念
// 统配符 “_”表示任意字符,“_*”表示任意长度
def strMatch(): Unit = {
val hi = Array( "hi" , "hello" , "world" )
hi match {
case Array( "hi" , _*) => println( "OK" ) //第一个pi匹配后,其它就不会匹配
case Array( "hi" , "world" , _) => println( "Wrong" )
case _ => println( "None" )
}
}
//类型匹配
def getType(a: Any): Unit = {
a match {
case _: Array[Char] => println ( "Array[Char]" )
case _: Int => println( "Int" )
case _: Char => println( "Char" )
case _ => println( "Error" )
}
}
// 样例类,经过优化,为了用做模式匹配
//定义一个抽象类
abstract class Expr
//继承自Expr
case class Number(n: Int) extends Expr
//继承自Expr
case class Sum(e1: Expr, e2: Expr) extends Expr
def getType2(a: Expr): String =
a match {
case Number(n) => "Number"
case Sum(m, n) => "Sum"
case _ => "Else"
}
// 样例类改进
abstract class Expr3 {
//写在类成员中进行简化
def getType3: String = this match {
case Number3(n) => "Number3"
case Sum3(m, n) => "Sum3"
case _ => "Else3"
}
}
//继承自Expr3
case class Number3(n: Int) extends Expr3
//继承自Expr3
case class Sum3(e1: Expr3, e2: Expr3) extends Expr3
// 总结样例类:默认生成toString,equals,hashCode,copy方法
// 封闭类,样本类的超类被封闭 sealed关键字,在类定义文件外不能添加子类
//定义一个抽象类
sealed abstract class Expr4
//继承自Expr
case class Number4(n: Int) extends Expr4
//继承自Expr
case class Sum4(e1: Expr4, e2: Expr4) extends Expr4
def main(args: Array[ String ]): Unit = {
strMatch()
getType( 5 )
getType( "abc" .toCharArray)
println( getType2( new Number( 5 )))
println( getType2( new Sum( new Number( 5 ), new Number( 5 ))))
println( getType2( new Expr {}))
val hi = new Number3( 6 )
val hello = new Sum3( new Number3( 5 ), new Number3( 5 ))
println(hi.getType3)
}
/**
* Created by admin on 2016/1/10.
*/
object MatchTest {
//模式匹配是数据结构上的概念
// 统配符 “_”表示任意字符,“_*”表示任意长度
def strMatch(): Unit = {
val hi = Array( "hi" , "hello" , "world" )
hi match {
case Array( "hi" , _*) => println( "OK" ) //第一个pi匹配后,其它就不会匹配
case Array( "hi" , "world" , _) => println( "Wrong" )
case _ => println( "None" )
}
}
//类型匹配
def getType(a: Any): Unit = {
a match {
case _: Array[Char] => println ( "Array[Char]" )
case _: Int => println( "Int" )
case _: Char => println( "Char" )
case _ => println( "Error" )
}
}
// 样例类,经过优化,为了用做模式匹配
//定义一个抽象类
abstract class Expr
//继承自Expr
case class Number(n: Int) extends Expr
//继承自Expr
case class Sum(e1: Expr, e2: Expr) extends Expr
def getType2(a: Expr): String =
a match {
case Number(n) => "Number"
case Sum(m, n) => "Sum"
case _ => "Else"
}
// 样例类改进
abstract class Expr3 {
//写在类成员中进行简化
def getType3: String = this match {
case Number3(n) => "Number3"
case Sum3(m, n) => "Sum3"
case _ => "Else3"
}
}
//继承自Expr3
case class Number3(n: Int) extends Expr3
//继承自Expr3
case class Sum3(e1: Expr3, e2: Expr3) extends Expr3
// 总结样例类:默认生成toString,equals,hashCode,copy方法
// 封闭类,样本类的超类被封闭 sealed关键字,在类定义文件外不能添加子类
//定义一个抽象类
sealed abstract class Expr4
//继承自Expr
case class Number4(n: Int) extends Expr4
//继承自Expr
case class Sum4(e1: Expr4, e2: Expr4) extends Expr4
def main(args: Array[ String ]): Unit = {
strMatch()
getType( 5 )
getType( "abc" .toCharArray)
println( getType2( new Number( 5 )))
println( getType2( new Sum( new Number( 5 ), new Number( 5 ))))
println( getType2( new Expr {}))
val hi = new Number3( 6 )
val hello = new Sum3( new Number3( 5 ), new Number3( 5 ))
println(hi.getType3)
}
}