Scala provides a powerful pattern matching mechanism and has a wide range ofapplications.
A pattern match contains a series of alternatives, each starting with a keyword case . Each alternative contains a pattern and one or more expressions. The arrow symbol => patterns and expressions are separated.
The following is a simple example of integer value pattern matching: Execute the above code, and the output is as follows: Next, let’s look at a pattern matching of different data types: Execute the above code, and the output is as follows: The first in the instance The class definition that uses the case keyword is the case classes, which is a special class that is optimized for pattern matching. The following is a simple example of the sample class: Execute the above code, and the output is as follows: The following process occurs automatically when the sample class is declared: Each parameter of the constructor becomes Provided in the companion object Provide Generate
最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。 8.38.1. Example #
object Test { def main(args: Array[String]) { println(matchTest(3)) } def matchTest(x: Int): String = x match { case 1 => "one" case 2 => "two" case \_ => "many" } }
$ scalac Test.scala $ scala Test many
Match corresponds to switch in Java, but is written after the selector expression. Namely: selector match {alternative}. match expressions are evaluated by trying each pattern in the order in which they are written, as long as a matching case , the rest. case will not continue to match. 8.38.2. Example #
object Test { def main(args: Array[String]) { println(matchTest("two")) println(matchTest("test")) println(matchTest(1)) println(matchTest(6)) } def matchTest(x: Any): Any = x match { case 1 => "one" case "two" => 2 case y: Int => "scala.Int" case \_ => "many" } }
$ scalac Test.scala $ scala Test 2 many one scala.Int
case corresponding to the integer value 1, the second case , the corresponding string value two, the third case .The corresponding type pattern, which is used to determine whether the passed value is an integer, compared to using the isInstanceOf to determine the type, it is better to use pattern matching. The fourth case indicates the default full-match option, that is, if no other matchis found, similar to switch in default .Use sample classes #
8.38.3. Example #
object Test { def main(args: Array[String]) { val alice = new Person("Alice", 25) val bob = new Person("Bob", 32) val charlie = new Person("Charlie", 32) for (person <- < span> List(alice, bob, charlie)) { person match { case Person("Alice", 25) => println("Hi Alice!") case Person("Bob", 32) => println("Hi Bob!") case Person(name, age) => println("Age: " + age + " year, name: " + name + "?") } } } // Sample Class case class Person(name: String, age: Int) }
$ scalac Test.scala $ scala Test Hi Alice! Hi Bob! Age: 32 year, name: Charlie?
val unless explicitly declared as var , but this is not recommended apply method, so you don’t have to use the new keyword to build an object unapply method to make pattern matching work toString 、 equals 、 hashCode and copy methods, unless the definition of these methods is shown.