Scala Pattern Match之Case Class

Scala Pattern Match之Case Class

关于case class

http://my.oschina.net/xinxingegeya/blog/399977

如下代码所示,

case class Address(street: String, city: String, country: String)
case class Person(name: String, age: Int, address: Address)

val alice = Person("Alice", 12, Address("1 hello world", "2 hello world", "CN"))
val bob = Person("Bob", 12, Address("1 hello world", "2 hello world", "CN"))
val charlie = Person("Charlie", 12, Address("1 hello world", "2 hello world", "CN"))

for (person <- Seq(alice, bob, charlie)) {
  person match {
    case Person("Alice", 12, Address(_, "2 hello world", _)) => println("hi alice")
    case Person("Bob", 12, Address("1 hello world", "2 hello world", "CN")) => println("hi bob")
    case Person(name, age, _) => println("who are you?")
  }
}

=======END=======

你可能感兴趣的:(Scala Pattern Match之Case Class)