Scala编程中的=>和<=区别

来源:互联网 发布:java线程安全集合类 编辑:程序博客网 时间:2024/06/11 04:52

<=含义

小于等于号

=>的使用
1、当定义一个函数的时候,需要使用=>

scala> val triple = (x: Int) => 3 * x //定义了一个函数triple: Int => Int = <function1>scala> def square(x: Int) = x * x //定义了一个方法square: (x: Int)Intscala> triple(3)res1: Int = 9scala> square(3)res2: Int = 9

2、模式匹配

object MatchTest extends App {def matchTest(x: Int): String = x match {  case 1 => "one"  case 2 => "two"  case _ => "many"}println(matchTest(3))}