Scala language pattern matching (6)

Source: Internet
Author: User

==> 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.