-
=>
是函数的标志,看到这个符号,就要想到函数。常见的场景有两种:- 定义匿名函数
// 1. 直接定义匿名函数 val func = (x: Double) => 3 * x // 2. 将匿名函数做为实参传递 sc.map(x => 3 * x)
- 表示函数的参数类型和返回值类型
def test(f: Double => Double, n1: Double) = { ... }
- 定义匿名函数
-
=>
在模式匹配中使用object MatchDemo01 { def main(args: Array[String]): Unit = { val oper = '+' val n1 = 20 val n2 = 10 var res = 0 /** * 1、match(类似于java中的switch)和case是关键字 * 2、每个代码块不需要写break */ oper match { case '+' => res = n1 + n2 case '-' => res = n1 - n2 case '*' => res = n1 * n2 case '/' => res = n1 / n2 case _ => println("oper error") // 此时 _ 表示 else } //输出 res=30 println("res=" + res) } }
- => 等价于 java swtich 的
:
- => 后面的代码块到下一个 case, 是作为一个整体执行,可以使用{}扩起来,也可以不扩。
- => 等价于 java swtich 的
【Scala】=>的含义
于 2023-06-01 17:17:24 首次发布