Chinaunix首页 | 论坛 | 博客
  • 博客访问: 371462
  • 博文数量: 85
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 657
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-17 20:48
个人简介

行到水穷处,坐看云起时

文章分类

全部博文(85)

文章存档

2019年(2)

2018年(1)

2016年(1)

2015年(66)

2014年(15)

我的朋友

分类: Java

2015-04-01 10:34:40

Case class
case class和普通class类似,只是定义的时候以case class开始,而不是class。对于case class,编译器自动添加伙伴object,伙伴object中包含apply和unapply方法,apply提供了简化构造实例的方式(可以省略new关键字),unapply提供了对class实例属性提取的方法。

Scala中,对于X(param), x为object名称或者class实例对象,编译器会将其转换成 x.apply(param)的调用,所以case class的伙伴object中的apply方法,提供了类似工厂方法的构造class实例的功能。同时,case class构造函数中的参数,被编译器自动添加了val的前缀,成为case class的成员。对于case class,编译器同时添加了toString、hashCode、equals、copy方法。copy方法提供了从已有实例构造新实例的一种方法,只需要在参数列表中用命名参数指出需要改变的属性。实例如下:

scala> case class People(firstName:String,lastName:String)
defined class People

scala> val people = People("Alice","Wang")
people: People = People(Alice,Wang)

scala> people.firstName
res0: String = Alice

scala> people.lastName
res1: String = Wang

scala> people == People("Alice","Wang")
res5: Boolean = true

scala> val miss = people.copy(lastName="Ma")
miss: People = People(Alice,Ma)

scala> println(miss)
People(Alice,Ma)

scala> println(people)
People(Alice,Wang)

Pattern matching
Scala中,模式匹配和Java中的switch case比较类似,具体形式如下:
x match {
 case expr => expr
 }
x为变量。
比如:

scala> val v = 1
v: Int = 1

scala> v match {
     |  case 1 => println("One")
     |  case _ => println("Not one")
     | }
One

其中,case _和 匹配其它所有条件;case 语句从上往下执行,只执行匹配到的case;和Java中不同,不需要指定break。比如:

scala> def matchTest(x:Any) = x match {
     |   case 1 => println("Got one")
     |   case v:Int => println("Got Int "+ v)
     |   case _ =>
     | }
matchTest: (x: Any)Unit

scala> matchTest(1)
Got one

scala> def matchTest(x:Any) = x match {
     |   case v:Int => println("Got Int " + v)
     |   case 1 => println("Got one")
     |   case _ =>
     | }
matchTest: (x: Any)Unit

scala> matchTest(1)
Got Int 1

模式匹配有多种类型,比如:常量,类型,元组,构造函数等,比如:

scala> def matchTest(x:Any) = x match {
     |    case 1 => println("Got one")
     |    case str:String => println("Got string:" + str)
     |    case (a,b,c) => println("Got triple:("+a+","+b+","+c+")")
     |    case People(firstName,lastName) => println("Got people:"+firstName+" "+lastName)
     |    case _ => println("Got something new")
     | }
matchTest: (x: Any)Unit

scala> matchTest(1)
Got one

scala> matchTest("Hi,Alice")
Got string:Hi,Alice

scala> matchTest((1,2,3))
Got triple:(1,2,3)

scala> val alice = People("Alice","Wang")
alice: People = People(Alice,Wang)

scala> matchTest(alice)
Got people:Alice Wang

scala> matchTest(Nil)
Got something new

Pattern Guards
在匹配模式的时候,可以进一步添加guard(一个boolean表达式),指定模式适用的条件。比如:

scala> def matchTest(x:Any) = x match {
     |  case People(firstName,lastName) if firstName=="Alice" => println("Hello,Alice")
     |  case _ => println("Glad to see you")
     | }
matchTest: (x: Any)Unit

scala> val alice = People("Alice","Wang")
alice: People = People(Alice,Wang)

scala> val tudou = People("Tudou","Ma")
tudou: People = People(Tudou,Ma)

scala> matchTest(alice)
Hello,Alice

scala> matchTest(tudou)
Glad to see you

参考自:
       Programming Scala 2nd Edition
阅读(1328) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~