==> Scala has a powerful pattern-matching mechanism, similar to a switch statement , that allows for the determination of types and checks, as well as a sample class that enables pattern matching of objects of a sample class
==> Pattern Matching Example
var V1 = 0var Ch1 = "-" ch1 match{case "+" + v1 = 1 case "-" = = =-1 Case _ = = V1 (v1)
==> Scala Guard
Scala Guard matches all values of a certain type var v2 = ' 6 ' var ch2:int = -1v2 match{case ' + ' + = println ("This is A +") case '-' "and" println ("This is a -") Case _ If Character.isdigit (v2) = CH2 = Character.digit (CH2, ten) Case _ = = println (" other type ")}println (" CH2: "+ CH2)
==> a variable in a pattern match
The pattern matches the variable var v3 = "Hello" v3 (1) match{case ' + ' = = println ("This is A +") case '-' = ' + println ("This is a-") Case ch = println ("This is" + ch)}
==> arrays and lists in pattern matching
// matching array and list Var myarray = array (all in a) myarray Match { case array (0) => println ("0") case array (X, y) => println ("This is a list that contains two elements, and is: " + (x+y)) case array (X, y,  Z) => println ("contains three elements, product is: " + (x*y*z)) case array (x, _ *) => println ("This is an array")}var mylist = list (1,2,3,4) mylist match{ Case list (0) => println ("0") case list (x, y) => println (" This is a two element, the product is: " + (X+y)) case list (x, y, z) => println (" This is an array of three elements, the product is: " + (x*y*z)) case list (_*) => println (" This is a multivariate prime group " )}
==> Sample Class (Caseclass) with the case keyword in front of the class, the greatest benefit of which is to support pattern recognition
Determine whether an object belongs to class var acar:democaseclass = new Bicycle ("Car") ACar match{case Car (name) and println ("I am a Car") CAs by using the sample class e Bicycle (name) = println ("I am a zixingche|") Case _ = = println ("other")}class Democaseclasscase class Car (name:string) extends Democaseclasscase class Bicycle ( name:string) extends Democaseclass
Scala language pattern matching (6)