Skip to content

Latest commit

 

History

History
122 lines (89 loc) ยท 6.02 KB

case-classes.md

File metadata and controls

122 lines (89 loc) ยท 6.02 KB
layout title partof num language next-page previous-page
tour
์ผ€์ด์Šค ํด๋ž˜์Šค
scala-tour
11
ko
pattern-matching
multiple-parameter-lists

์Šค์นผ๋ผ๋Š” ์ผ€์ด์Šค ํด๋ž˜์Šค ๊ฐœ๋…์„ ์ง€์›ํ•œ๋‹ค. ์ผ€์ด์Šค ํด๋ž˜์Šค๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๋Š” ์ผ๋ฐ˜ ํด๋ž˜์Šค์ด๋‹ค.

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถˆ๋ณ€
  • ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•ด ๋ถ„ํ•ด๊ฐ€๋Šฅ
  • ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ์•„๋‹Œ ๊ตฌ์กฐ์ ์ธ ๋™๋“ฑ์„ฑ์œผ๋กœ ๋น„๊ต๋จ
  • ์ดˆ๊ธฐํ™”์™€ ์šด์˜์ด ๊ฐ„๊ฒฐํ•จ

์ถ”์ƒ ์ƒ์œ„ ํด๋ž˜์Šค Notification๊ณผ ์„ธ๊ฐœ์˜ ํŠน์ • Notification ํƒ€์ž…๋“ค(์ผ€์ด์Šค ํด๋ž˜์Šค 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

์ผ€์ด์Šคํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ๋‹ค (new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Œ์„ ์ฃผ๋ชฉํ•˜์ž.)

val emailFromJohn = Email("john.doe@mail.com", "Greetings From John!", "Hello World!")

์ผ€์ด์Šค ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์€ public ๊ฐ’์œผ๋กœ ๋‹ค๋ค„์ง€๋ฉฐ, ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

val title = emailFromJohn.title
println(title) // prints "Greetings From John!"

์—ฌ๋Ÿฌ๋ถ„์€ ์ผ€์ด์Šค ํด๋ž˜์Šค์˜ ํ•„๋“œ๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. (ํ•„๋“œ ์•ž์— var๋ฅผ ๋„ฃ์œผ๋ฉด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ถŒ์žฅ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.)

emailFromJohn.title = "Goodbye From John!" // ์ด๊ฒƒ์€ ์ปดํŒŒ์ผ์‹œ์— ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. ์šฐ๋ฆฌ๋Š” val์ธ ํ•„๋“œ์— ๋‹ค๋ฅธ ๊ฐ’์„ ํ• ๋‹นํ• ์ˆ˜ ์—†์œผ๋ฉฐ, ๋ชจ๋“  ์ผ€์ด์Šค ํด๋ž˜์Šค ํ•„๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ val์ด๋‹ค.

๋Œ€์‹ ์—, ๋‹น์‹ ์€ 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!)"

๋ชจ๋“  ์ผ€์ด์Šค ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ ์Šค์นผ๋ผ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ตฌ์กฐ์  ๋™๋“ฑ์„ฑ์„ ๊ตฌํ˜„ํ•œ 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!)

์ผ€์ด์Šค ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด, ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜๋Š” ํŒจํ„ด๋งค์นญ์„ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค. ์–ด๋–ค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) =>
      "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

์•„๋ž˜์— if ๋ฐฉ์–ด๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋‹ค๋ฅธ ์˜ˆ์ œ๊ฐ€ ์žˆ๋‹ค. if ๋ฐฉ์–ด๊ตฌ๋ฌธ์„ ํ†ตํ•ด, ํŒจํ„ด์ด ์ผ์น˜ํ•˜๋Š” ๋ถ„๊ธฐ๋ฌธ์€ ๋ฐฉ์–ด๊ตฌ๋ฌธ์•ˆ์˜ ์กฐ๊ฑด์ด false๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ์‹คํŒจํ•œ๋‹ค.

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 someSms = SMS("12345", "Are you there?")
val someVoiceRecording = 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(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))

// 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!

์Šค์นผ๋ผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์žˆ์–ด์„œ, ๋ณดํ†ต model/group ๋ฐ์ดํ„ฐ์— ์ผ€์ด์Šค ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒ์žฅ๋˜๋Š”๋ฐ, ๋‹น์‹ ์ด ๋”์šฑ ํ‘œํ˜„์ ์ด๊ฑฐ๋‚˜ ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋„์›€์ด ๋œ๋‹ค.

  • ๋ถˆ๋ณ€์„ฑ์€ ๋‹น์‹ ์ด ์–ธ์ œ ์–ด๋””์„œ ๊ทธ๊ฒƒ๋“ค์ด ์ˆ˜์ •๋˜๋Š”์ง€ ์‹ ๊ฒฝ์“ธ ํ•„์š” ์—†๊ฒŒ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
  • ๊ฐ’์„ ํ†ตํ•œ ๋น„๊ต๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ธ์Šคํ„ด์Šค๋“ค์„ ์›์‹œ ๊ฐ’๋“ค์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋น„๊ตํ• ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด ์ค€๋‹ค - ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋“ค์ด ๊ฐ’ ๋˜๋Š” ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ๋น„๊ต๋˜๋Š”์ง€์™€ ๊ฐ™์€ ๋ถˆํ™•์‹ค์„ฑ์„ ์ œ๊ฑฐ
  • ํŒจํ„ด๋งค์นญ์€ ๋กœ์ง์˜ ๋ถ„๊ธฐ๋ฅผ ์‹ฌํ”Œํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋ฉฐ, ๊ฒฐ๊ตญ ์ ์€ ๋ฒ„๊ทธ์™€ ๊ฐ€๋…์„ฑ ๋†’์€ ์ฝ”๋“œ๋กœ ์ด์–ด์ง„๋‹ค.

๊ณ ๊ด‘ํ˜„ ์˜ฎ๊น€