Scala中模式匹配的方法是什么

2024-04-15

在Scala中,模式匹配的方法主要有两种:使用match关键字和case语句进行模式匹配,以及使用match方法和case语句进行模式匹配。具体示例如下:

  1. 使用match关键字和case语句进行模式匹配:
val x: Any = 10

x match {
  case 1 => println("One")
  case 2 => println("Two")
  case _ => println("Other")
}
  1. 使用match方法和case语句进行模式匹配:
val x: Any = 10

x match {
  case 1 => println("One")
  case 2 => println("Two")
  case _ => println("Other")
}