// case classes
// 一种特别的类,
// 默认不可变/可以通过模式匹配分解/关于比较/实例起来很简洁=>不需要使用new
abstract class Notification
case class Email(sourceEmail:
String, title:
String, body:
String) extends Notification
case class SMS(sourceNumber:
String, var message:
String) extends Notification
case class VoiceRecording(contactName:
String, link:
String) extends Notification
// 不需要使用new 实例化时很简洁
val emailFromJohn = Email("john.doe@mail.com", "Greetings From John!", "Hello World!")
val smsFromJohn = SMS("john.doe@mail.com", "Greetings From John!")
val title = emailFromJohn.title// 默认是公有的
println(title)
// 但是不可变的 因此不能重写<除非开始的时候申明为var 但不鼓励这样 >
emailFromJohn.title
= "Goodbye From John!" // This is a compilation
smsFromJohn.message = "ok"
// 但是可以借助copy方法 , 拷贝的过程中, 改写原来的
val editedEmail = emailFromJohn.copy(title = "I am learning Scala!", body = "It's so cool!")
println(emailFromJohn) // prints "Email(john.doe@mail.com,Greetings From John!,Hello World!)"
println(editedEmail) // prints "Email(john.doe@mail.com,I am learning Scala,It's so cool!)"
// 比较相等 是比较每个成员的值
// case类一定会实现 == 和 tostring
val firstSms = SMS("12345", "Hello!")
val secondSms = SMS("12345", "Hello!")
if (firstSms ==
secondSms) {
println("They are equal!")
}
println("SMS is: " +
firstSms)
// 还可以使用模式匹配 这样表达更简洁
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) =>
"you received a Voice Recording from " +
name +
"! Click the link to hear it: " +
link
}
}
val someSms = SMS("12345", "Are you there?")
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
println(showNotification(someSms))
println(showNotification(someVoiceRecording))
// prints:
// 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
def showNotificationSpecial(notification:
Notification, specialEmail:
String, specialNumber:
String):
String = {
notification match {
case Email(email, _
, _
) if email ==
specialEmail =>
"You got an email from special someone!"
case SMS(number, _
) if number ==
specialNumber =>
"You got an SMS from special someone!"
case other =>
showNotification(other) // nothing special, delegate to our original showNotification function
}
}
val SPECIAL_NUMBER = "55555"
val SPECIAL_EMAIL = "jane@mail.com"
val someSms2 = SMS("12345", "Are you there?")
val someVoiceRecording2 = VoiceRecording("Tom", "voicerecording.org/id/123")
val specialEmail = Email("jane@mail.com", "Drinks tonight?", "I'm free after 5!")
val specialSms = SMS("55555", "I'm here! Where are you?")
println(showNotificationSpecial(someSms2, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(someVoiceRecording2, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(specialEmail, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(specialSms, SPECIAL_EMAIL, SPECIAL_NUMBER))
// prints:
// 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!
转载请注明原文地址: https://www.6miu.com/read-8489.html