Scala中的case

一:模式匹配

1.简单匹配,值匹配
val bools = List(true, false)
for (bool <- bools) {
bool match {
case true => println("heads")
case false => println("tails")
case _ => println("something other than heads or tails (yikes!)")
}
}
2.类型匹配
val sundries = List(23, "Hello", 8.5, 'q')
 for (sundry <- sundries) {
  sundry match {
   case i: Int => println("got an Integer: " + i)
   case s: String => println("got a String: " + s)
   case f: Double => println("got a Double: " + f)
   case other => println("got something else: " + other)
 }
}
3.根据顺序匹配
val willWork = List(1, 3, 23, 90)
val willNotWork = List(4, 18, 52)
val empty = List()
for (l <- List(willWork, willNotWork, empty)) {
 l match {
   case List(_, 3, _, _) => println("Four elements, with the 2nd being '3'.")
   case List(_*) => println("Any other list with 0 or more elements.")
 }
}
4. guard 的数组匹配
val tupA = ("Good", "Morning!")
val tupB = ("Guten", "Tag!")
for (tup <- List(tupA, tupB)) {
 tup match {
  case (thingOne, thingTwo) if thingOne == "Good" =>
    println("A two-tuple starting with 'Good'.")
  case (thingOne, thingTwo) =>println("This has two things: " + thingOne + " and " + thingTwo)
 }
}
5.对象深度匹配:

case class Person(name: String, age: Int)
val alice = new Person("Alice", 25)
val bob = new Person("Bob", 32)
val charlie = new Person("Charlie", 32)
for (person <- List(alice, bob, charlie)) {
 person match {
  case Person("Alice", 25) => println("Hi Alice!")
  case Person("Bob", 32) => println("Hi Bob!")
  case Person(name, age) =>
     println("Who are you, " + age + " year-old person named " + name + "?")
 }
}

6.正则表达式匹配
val BookExtractorRE = """Book: title=([^,]+),\s+authors=(.+)""".r
val MagazineExtractorRE = """Magazine: title=([^,]+),\s+issue=(.+)""".r

val catalog = List(
 "Book: title=Programming Scala, authors=Dean Wampler, Alex Payne",
 "Magazine: title=The New Yorker, issue=January 2009",
 "Book: title=War and Peace, authors=Leo Tolstoy",
 "Magazine: title=The Atlantic, issue=February 2009",
 "BadData: text=Who put this here??"
)

for (item <- catalog) {
 item match {
  case BookExtractorRE(title, authors) =>
     println("Book \"" + title + "\", written by " + authors)
  case MagazineExtractorRE(title, issue) =>
     println("Magazine \"" + title + "\", issue " + issue)
  case entry => println("Unrecognized entry: " + entry)
 }
}

二:类class前面声明为case class

class之前添加case可以自动生成equal、hashcode、toString、copy方法 
和他的伴生对象,并且为半生对象生成apply、unapply方法。

import scala.language.postfixOps
case class ForFun(name:String)
object fun2{
  def main(args: Array[String]): Unit = {
    val forfun = ForFun.apply("Jack")
    println(forfun.name)
    val forFun2 = new ForFun("Nacy")
    println(forFun2.name+":"+forFun2.hashCode()+":"+forFun2.toString)
    println(forfun equals forFun2)
  }
}
结果如下:
Jack
Nacy:-1811736370:ForFun(Nacy)
false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值