scala语言的一大重要特性之一就是模式匹配。在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中,只能是用在函数体类的,且需要结合break使用。但是,在scala语言中,pattern matching除了有case关键字,其他,似乎和java又有很多甚至完全不同的东西。

scala在消息处理中为模式匹配提供了强大的支持!

下面看看模式匹配的英文定义:

A pattern match includes a sequence of alternatives, each starting with the keyword case. Each alternative includes a pattern and one or more expressions, which will be evaluated if the pattern matches. An arrow symbol => separates the pattern from the expressions.

再来给个简单的例子吧:

 package lession3

 object MyPattern {
def main(args: Array[String]) {
println(matchTest(3)) }
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
}

上面的例子,case部分,是不是很像java中switch语句中的case?下面给个对应的简单的java例子:

 package com.shihuc.java;

 public class MySwitch {
public static void main(String args[]){
System.out.println(matchTest(3));
} public static String matchTest(int x){
String ret = "";
switch(x){
case 1:
ret = "one";
break;
case 2:
ret = "two";
break;
default:
ret = "many";
break;
}
return ret;
}
}

呵呵,是不是发现了相识和差异?都是一个模式对应一系列的候选可能表达(expressions,其实,就是一系列的处理过程),scala里面的case语句没有java switch中的break,scala里面的下划线_对应于java switch中的default,还有,是不是觉得那个x match很像java代码中的switch(x),当然了,学习一种语言,可以这么类比着学习!至少我是这么学的!

另外,java的switch语句中的x是数值类型(int或者enum)的参数,但是,scala中,是没有这么要求的!任意类型都可以,这个是不是很强大?

 object Test {
def main(args: Array[String]) {
println(matchTest("two"))
println(matchTest("test"))
println(matchTest(1)) }
def matchTest(x: Any): Any = x match {
case 1 => "one"
case "two" => 2
case y: Int => "scala.Int"
case _ => "many"
}
}

还有,scala的模式匹配书写格式,还可以如下这样:

 object Test {
def main(args: Array[String]) {
println(matchTest("two"))
println(matchTest("test"))
println(matchTest(1)) }
def matchTest(x: Any){
x match {          //此处的x match关键部分,不再写在函数名称后面了,而是写在了函数体里面,即在函数块{}括号里面!
case 1 => "one"
case "two" => 2
case y: Int => "scala.Int"
case _ => "many"
}
}
}

再者,scala还支持模式匹配类,即在正常class定义中加入关键字case, 看下面的例子:

 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 <- 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 + "?")
}
}
}
// case class, empty one.
case class Person(name: String, age: Int)
}

case关键字将会促使scala编译器为Person类自动加入一些模式匹配的特性。

首先,自动将Person类的参数类型转换为不可变(immutable)类型(即val,注意,val是可选的,此时);若想用可变的参数,则需要显示表明参数类型为var。

其次,编译器自动将为Person类添加了例如equalshashCode,以及toString等class方法,我们不需要自己再写相关的方法了。

scala的异常处理,也会涉及到case的使用,即各种细分的异常的模式匹配。这个与java中的try/catch()之多个catch()体不同:

JAVA try catch:

 package com.shihuc.java;

 import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class FileOpen2 { public static void main(String args[]){
try {
FileReader fr = new FileReader("./test.txt");
char buf[] = new char[1024];
while(fr.read(buf) >= 0){
System.out.println(buf);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Scala try catch:

 import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException object Test {
def main(args: Array[String]) {
try {
val f = new FileReader("input.txt")
} catch {
case ex: FileNotFoundException =>{
println("Missing file exception")
}
case ex: IOException => {
println("IO Exception")
}
}
}
}

scala pattern matching的更多相关文章

  1. [Scala] Pattern Matching(模式匹配)

    Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...

  2. learning scala pattern matching 03

    code: package com.aura.scala.day01 object patternMatching03 { //当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用. def g ...

  3. learning scala pattern matching 02

    code package com.aura.scala.day01 object patternMatching02 { def main(args: Array[String]): Unit = { ...

  4. learning scala pattern matching

    code: package com.aura.scala.day01 import scala.util.Random object patternMatching01 { def main(args ...

  5. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

  6. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  7. Zhu-Takaoka Two-dimensional Pattern Matching

    Two dimensional pattern matching. Details may be added later.... Corresponding more work can be foun ...

  8. [PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching

    Pattern matching in functional programming languages is a way to break up expressions into individua ...

  9. pattern matching is C# 7.0

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is 原来的版本 private static s ...

随机推荐

  1. Magento的价格去掉小数点

    Magento的默认情况,价格后面是有小数点的,我们来看下如何正确的来去掉小数点. 1.复制如下路径的文件 app/code/core/Mage/Directory/Model/Currency.ph ...

  2. 《JavaScript Ninja》之函数是根基

    函数是根基 理解函数为什么如此重要 JavaScript 是一门 函数式语言 . 函数为什么是第一型对象 在 JavaScript 中,函数可以共处,可以将其视为其他任意类型的 JavaScript ...

  3. 在hdfs上存取xml文件的实现代码

    要读取的文件为:/user/hdfs/stdin.xml <?xml version="1.0" encoding="UTF-8"?> <re ...

  4. Core Java Volume I — 3.8. Control Flow

    3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...

  5. keynotes egestas,PPT 渐变背景下载-imsoft.cnblogs

  6. yii点击上传图片后立即显示

    结合yii上传做的图片上传后立即显示,自己琢磨的,有点low <script type="text/javascript">//下面用于图片上传预览功能function ...

  7. yii2构造方法

    控制器文件下面建 一公共控制器:: CommonController.php 里面写一个空方法  :  public function init(){} 然后在你要写构造函数的控制器类里写一个方法 ( ...

  8. tyvj1018 - 阶乘统计 ——暴力

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1018 范围只有20,在long long Int范围内. #include <cstdio> ...

  9. leetcode 131. Palindrome Partitioning----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  10. Linux安全攻防笔记

    一.上传木马的过程 1.默认端口22弱口令暴力破解: 2.21端口或者3306端口弱口令暴力破解: 3.webshell进行shell反弹提权: 4.木马传入服务器的上面并且执行,通过木马的方式来控制 ...