Case Classes
Scala 支持 case classes 记法。Case Class 就是普通的类, 除了:
- 默认不可变
- 可以通过模式匹配拆分
- 通过结构相等比较而非通过引用比较
- 易于实例化和操作
下面是一个 Notification 类型等级的例子, 它由一个抽象的超类 Notification 和三个具体的用 case classes Email
, SMS
, VoiceRecording
实现的 Notification 类型。
abstract class Notification
case class Email(sourceEmail: String, title: String, body: String) extends Notification
case class SMS(sourceNumber: String, message: String) extends Notification
case class VoiceRecording(contactName: String, link: String) extends Notification
实例化一个 case class 很容易:(注意我们不需要使用 new
关键字)
val emailFromJohn = Email("[email protected]", "Greetings From John!", "Hello World!")
case classes 的构造函数参数被当作公共值, 可以被直接访问。
val title = emailFromJohn.title
println(title) // 打印 "Greetings From John!"
使用 case classes, 你不能直接改变它们的字段(field)。(除非你在字段前插入 var
, 但是并不鼓励这样做)。
emailFromJohn.title = "Goodbye From John!" // 这会导致编译器错误,我们不能将另一个值赋值给一个不可变的字段, 其中所有的 case classes 字段默认都是不可变的。
相反, 你使用 copy
方法来做一份拷贝。如下所示, 你可以替换某些字段:
val editedEmail = emailFromJohn.copy(title = "I am learning Scala!", body = "It's so cool!")
println(emailFromJohn) // prints "Email([email protected],Greetings From John!,Hello World!)"
println(editedEmail) // prints "Email([email protected],I am learning Scala,It's so cool!)"
对于每个 case classes, Scala 编译器生成一个实现了结构性相等的 equals
方法和 toString
方法。例如:
val firstSms = SMS("12345", "Hello!")
val secondSms = SMS("12345", "Hello!")
if (firstSms == secondSms) {
println("They are equal!")
}
println("SMS is: " + firstSms)
它会打印:
They are equal!
SMS is: SMS(12345, Hello!)
使用 case classes, 你可以使用 模式匹配来操作你的数据。这儿有一个函数, 它根据所检索的 Notification 的类型打印不同的信息:
def showNotification(notification: Notification): String = {
notification match {
case Email(email, title, _) => "You got an email from " + email + " with title: " + title
case SMS(number, message) => "You got an SMS from " + number + "! Message: " + message
case VoiceRecording(name, link) => "你收到一段来自" + name + "的录音!点击链接 " + link + " 收听它"
}
}
val someSms = SMS("12345", "Are you there?")
val someVoiceRecoding = VoiceRecoding("Tom", "voicerecoding.org/id/123")
println(showNotification(someSms))
println(showNotification(someVoiceRecoding))
// 打印
/ You got an SMS from 12345! Message: Are you there?
// 你收到一段来自 Tom的语音!点击链接 voicerecording.org/id/123 收听它
下面有一个更复杂的使用 if
guards 的例子。使用 if
guard, 如果 guard 中的条件返回 false, 那么模式匹配分支会失败。
def showNotificationSpecial(
notification: Notification,
specialEmail: String,
specialNumber: String): String = {
notification match {
case Email(email, _, _) if email == specialEmail => "你有一封来自特别关注人的邮件!"
case SMS(number, _) if number == specialNumber => "你有一条来自特别关注人的短信!"
case other => showNotification(other) // 没有特殊, 代理给我们原来的 showNotification 函数
}
}
val SPECIAL_NUMBER = "55555"
val SPECIAL_EMAIL = "[email protected]"
val someSms = SMS("12345", "Are you there?")
val someVoiceRecoding = VoiceRecoding("Tom", "voicerecording.org/id/123")
val specialEmail = Email("[email protected]", "Drinks tonight?", "I'm free after 5!")
val specialSms = SMS("55555", "I'm here! Where are you?")
println(showNotificationSpecial(someSms, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(someVoiceRecording, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(specialEmail, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(specialSms, SPECIAL_EMAIL, SPECIAL_NUMBER))
// 打印:
// You got an SMS from 12345! Message: Are you there?
// you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123
// You got an email from special someone!
// You got an SMS from special someone!
当使用 Scala 编程的时候, 推荐你使用 case classes 来对数据进行建模/分组, 因为它们有助于编更具表达性和可维护性的代码:
- 不可变性使你无需跟踪变化的时间和地点
- 按值比较可以让你比较实例, 就像它们是原始值一样, 没有更多的关于类的实例是按值还是按引用进行比较的不确定性
- 模式匹配简化了分支的逻辑, 这使代码不易出错,代码更可读。