一、方法
1 方法定义 2 Scala中 + - * / % 的作用和Java一样,但是特别的是,这些操作符实际上是方法。 3 1 to 10 4 1.to(10) 5 6 def m2(a:Int,b:Int): Int = a * b 7 8 def m1(a:Int,b:Int):Int = { 9 a + b 10 } 11 def:定义方法的关键字 12 m1:方法名 13 a:参数列表 14 b:参数列表 15 Int:返回值类型 16 a + b:函数体
二、函数
11)方式1 2 方法转换为函数 3 方式:方法名 _ 4 res6: (Int, Int) => Int = <function2> 5 <function2> 6 代表一个函数,并且有两个参数。 7 (Int, Int) 8 代表参数列表 9 Int 10 代表返回值类型 11 => 12 代表函数 13 14 2)方式2 15 定义函数: 16 val h1 = (a:Int,b:Int) => {a * b} 17 h1:函数名 18 (a:Int,b:Int):函数的参数类型 19 {a * b}:函数体
1、函数使用例子:
1//1、定义方法m2计算a和b的积 2scala> def m2(a:Int,b:Int):Int = a * b 3m2: (a: Int, b: Int)Int 4 5//2、调用方法m2 6scala> m2(4,6) 7res21: Int = 24 8 9//3、方法m2转换为函数 10scala> m2 _ 11res22: (Int, Int) => Int = <function2> 12 13//4、定义一个参数的方法m1 14scala> def m1(a:String) = println(a) 15m1: (a: String)Unit 16 17//5、调用方法m1 18scala> m1("hello world") 19hello world 20 21//6、方法m1转换为函数 22scala> m1 _ 23res24: String => Unit = <function1> 24 25//7、定义两个参数的函数(简写) 26scala> (a:Int,b:Int) => a * b 27res25: (Int, Int) => Int = <function2> 28 29//8、调用函数res25 30scala> res25(5,8) 31res26: Int = 40 32 33//9、定义两个参数的函数(标准写法) 34scala> (a:Int,b:Int) => {a * b} 35res27: (Int, Int) => Int = <function2> 36 37//10、定义函数h1 38scala> val h1 = (a:Int,b:Int) => {a * b} 39h1: (Int, Int) => Int = <function2> 40 41//11、调用函数h1 42scala> h1(4,9) 43res28: Int = 36
三、传值调用&传名调用
1 函数式编程:方法的参数可以是一个函数 2 传值调用: 3 1.先计算balance的值 4 2.把这个值作为参数传递给printMoney 5 传名调用:传递的是函数 6 将balance方法的名称传递到方法内部执行
1、传值调用
1package com.demo.scala02 2 3/** 4 * 传值调用 5 * 1.先计算balance的值 6 * 2.把这个值作为参数传递给printMoney 7 */ 8object ZFBToPay { 9 var money = 1000 10 11 // 吃饭一次花50元 12 def eat(): Unit = { 13 money = money - 50 14 } 15 16 // 余额 17 def balance(): Int = { 18 //已调用一次eat方法 19 eat() 20 money 21 } 22 23 def printMoney(x: Int): Unit = { 24 for(a <- 1 to 5){ 25 println(f"你的余额现在为:$x") 26 } 27 } 28 29 def main(args: Array[String]): Unit = { 30 eat() 31 balance() 32 printMoney(balance()) 33 } 34}
输出结果

2、传名调用
1package com.demo.scala02 2 3/** 4 * 传名调用 5 * 传递的是函数 6 * 将balance方法的名称传递到方法内部执行 7 */ 8object ZFBToPay2 { 9 var money = 1000 10 11 // 吃饭一次花50元 12 def eat(): Unit = { 13 money = money - 50 14 } 15 16 // 余额 17 def balance(): Int = { 18 // 已调用一次eat方法 19 eat() 20 money 21 } 22 23 // 此时余额减了5次 x: => Int 表示的是一个方法的签名 没有参数 返回值是Int类型的函数 24 def printMoney(x: => Int): Unit = { 25 for(a <- 1 to 5){ 26 println(f"你的余额现在为:$x") 27 } 28 } 29 30 def main(args: Array[String]): Unit = { 31 // eat() 32 // balance() 33 printMoney(balance()) 34 } 35}
输出结果

四、可变参数函数
1java中的可变参数:public static void m1(String ...arg){} 2 scala中的可变参数:def sum(ints:Int*): Int ={} 3 加了通配符*
1、java中的可变参数
1package com.demo.scala02; 2 3public class JavaTest { 4 // java中的可变参数 5 public static void m1(String ...args){ 6 7 } 8 9 public static void main(String[] args) { 10 m1("a"); 11 m1("a", "b", "c"); 12 } 13}
2、scala中的可变参数
1package com.demo.scala02 2 3object ManyParam { 4 // scala中的可变参数 5 def sum(ints: Int*): Int = { 6 var sum = 0 7 for(v <- ints){ 8 sum += v 9 } 10 sum 11 } 12 13 /* 14 不仅是可变参 15 而且参数的类型不一致 16 Any 17 */ 18 def setName(params: Any*): Any = { 19 return params 20 } 21 22 def main(args: Array[String]): Unit = { 23 println(sum(1,2,3,4)) 24 println(sum(2,3)) 25 println(setName("John",18,188)) 26 } 27}
输出结果

五、默认参数值函数
如果传递了参数,则使用传递的值。如果不传递参数,则使用默认值。
1、例子
1package com.demo.scala02 2 3object DefaultParam { 4 /* 5 如果调用此方法且不给参数,要一个默认值。 6 */ 7 def sum(a: Int = 3,b: Int = 7): Int = { 8 a + b 9 } 10 11 def main(args: Array[String]): Unit = { 12 // 如果传递了参数,则使用传递的值。如果不传递参数,则使用默认值。 13 println(sum()) 14 println(sum(2,4)) 15 } 16}
输出结果

六、高阶函数
定义:将其他函数作为参数,或者其结果是函数的函数。
1、例子
1package com.demo.scala02 2 3object GaoJie { 4 // 将其他函数作为参数,或者其结果是函数的函数 5 def getPerson(h: Int => String,f: Int): String = { 6 //函数h 参数f 7 h(f) 8 } 9 10 def Person(x: Int) = "我是" + x.toString + "岁很帅的lz" 11 12 def main(args: Array[String]): Unit = { 13 println(getPerson(Person,18)) 14 } 15}
七、部分参数应用函数
1 如果函数传递所有预期的函数,则表示完全应用它了。 2 如果只传递几个参数,并不是全部参数,那么将返回部分应用的函数。
1、例子
1package com.demo.scala02 2 3import java.util.Date 4 5object PartParam extends App { 6 def log(date: Date,message: String): Unit = { 7 //参数打印 8 println(s"$date,$message") 9 } 10 val date = new Date() 11 val logMessage = log(date,_:String) 12 13 log(date, "hello world") 14 logMessage("2018马上要过去了!") 15}
输出结果

2、命令行例子
1scala> val sum = (a:Int,b:Int) => {a + b} 2sum: (Int, Int) => Int = <function2> 3 4scala> sum(1,) 5<console>:1: error: illegal start of simple expression 6sum(1,) 7 ^ 8 9scala> sum(1,_:Int) 10res0: Int => Int = <function1> 11 12scala> res0(2) 13res1: Int = 3 14 15scala> res0(20) 16res2: Int = 21
八、字符串的格式化输出
文字插值器:f/s
1、例子
1scala> val name = "lz" 2name: String = lz 3 4scala> var age = 18 5age: Int = 18 6 7scala> println("name:"+name+" age:"+age) 8name:lz age:18 9 10scala> println(f"name=$name age=$age") 11name=lz age=18 12 13scala> printf(s"name=$name age=$age") 14name=lz age=18 15scala> println(s"name=$name age=$age") 16name=lz age=18 17 18scala> println(s"1 + 1 = ${1+1}") 191 + 1 = 2 20 21scala> println(f"1 + 1 = ${1+1}") 221 + 1 = 2
九、柯理化
1 指的是将原来接收的两个参数的函数变为一个新的接收一个参数的函数,这样的一个过程。 2 函数的通用性降低,但是适用性提高
1、例子
1scala> def sum(a:Int,b:Int) = a + b 2sum: (a: Int, b: Int)Int 3 4scala> def sum(a:Int)(b:Int) = a + b 5sum: (a: Int)(b: Int)Int 6 7scala> sum(1,2) 8<console>:13: error: too many arguments for method sum: (a: Int)(b: Int)Int 9 sum(1,2) 10 ^ 11 12scala> sum(1)(2) 13res11: Int = 3
十、偏函数
1 被包在花括号内没有match的一组case语句的是一个偏函数。 2 PartialFunction[A,B] 3 A: 参数类型 4 B:返回类型 5 常用与模式匹配。
1、例子
1package com.demo.scala02 2 3object PartialFunction { 4 5 //定义函数 6 def func(str: String): Int = { 7 if(str.equals("lz")) 18 else 0 8 } 9 10 //定义偏函数 11 def func1:PartialFunction[String, Int] = { 12 //如果使用了偏函数必须用case 13 case "lz" => 18 14 //如果其他 15 case _ => 0 16 } 17 18 def main(args: Array[String]): Unit = { 19 println(func("lz")) 20 println(func1("Lz")) 21 println(func1("lz")) 22 } 23}
输出结果
