原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-25/

There I am,in the world more exciting!

by postbird

www.ptbird.cn



package com.ptbird.scala /** * Created by postbird on 2016/5/26. * * 模式匹配 match */ object MacthTest { def main(args: Array[String]): Unit = { val data = 2 data match { case 1 => println("1") //右侧可以是一个函数 case 2 => println("2") case 3 => println("3") case 4 => println("4") case _ => println("other") } //other //scala中不需要使用break,scala自动的break或者return val result = data match { case i if i == 1 => "first " + i case number if number == 2 => "The Second" + number case _ => "not known" } println(result) //The Second2 //data的传递的参数可以进行通过一个变量进行接收并且对接收的变量进行使用 "Spark !".foreach { c => println( c match { case ' ' => "space" case ch => "Char: " + ch } ) } // Char: S // Char: p // Char: a // Char: r // Char: k // space // Char: ! } }