scala方法与函数
方法的定义及语法规则
-
方法与函数的区别
- Java:不区分函数和方法,因为Java是面向对象的
public 返回值 方法名(参数){ 方法体 }
-
方法:由方法名、参数、方法体构成,一般指的是类中定义的函数即为方法
-
函数:由函数名、参数、函数体构成,一般指的是可以独立构建的称为函数
-
Scala中定义方法的语法
def funcName(args1:Type1,args2:Type2……):ResultType = { //方法体 }
-
栗子:定义一个方法,实现传递两个Int参数,返回两个数值之和
scala> def m1(a:Int,b:Int):Int = { | a + b | } m1: (a: Int, b: Int)Int
-
规则
-
方法定义的关键字为def
-
如果方法体只有一行,{}可以省略
-
没有return关键字,使用方法体的最后一行【值或者表达式】作为返回值
-
大多数情况下返回值类型也可以自动推断,可以不写,递归方法除外
-
-
举例1:定义一个方法,实现传递两个Int参数,返回两个数值之和
scala> def m2(a:Int,b:Int):Int = a + b m2: (a: Int, b: Int)Int scala> m2(2,3) res9: Int = 5 scala> def m3(a:Int,b:Int) = a + b m3: (a: Int, b: Int)Int scala> m3(3,3) res10: Int = 6
-
举例2:定义一个方法,实现传递一个Int参数,返回这个Int值的阶乘
scala> def m4(x:Int) = { if(x<= 1) 1 else m4(x-1)*x } <console>:14: error: recursive method m4 needs result type def m4(x:Int) = { if(x<= 1) 1 else m4(x-1)*x } scala> def m4(x:Int):Int = { if(x<= 1) 1 else m4(x-1)*x } m4: (x: Int)Int scala> m4(2) res11: Int = 2 scala> m4(3) res12: Int = 6 scala> m4(4) res13: Int = 24 scala> m4(5) res14: Int = 120
-
方法定义的规则
- 关键字:def
- 没有return,用最后一行作为返回值
- 如果方法体只有一行,可以省略{}
- 大多数情况下,返回值类型可以不写,递归除外
方法的参数的使用
-
默认参数的方法定义
-
功能:在方法定义时,给定参数的默认值
-
语法
def funName(args1:Type1 = Value1,args2:Type2 = Value2 ……):ResultType = { //方法体 }
-
栗子:定义一个方法,实现传递两个Int参数,返回两个数值之和
def m5(a:Int = 0,b:Int = 2) = a + b
-
-
默认参数的方法调用
scala> def m5(a:Int = 0,b:Int = 2) = a + b m5: (a: Int, b: Int)Int scala> m5(1,1) res15: Int = 2 scala> m5(1,6) --指定所有参数 res16: Int = 7 scala> m5() --使用两个参数的默认值 res17: Int = 2 scala> m5(1) --给第一个参数赋值,第二个参数使用默认值 res18: Int = 3 scala> m5(a=1) res19: Int = 3 scala> m5(b=1) res20: Int = 1 scala> m5(b=4,a=3) --指定参数赋值 res21: Int = 7
-
变长参数的方法定义
-
功能:用于定义方法时传递不定个数的参数
-
语法
def funName(args1:Type1 *):ResultType = { //方法体 }
-
本质:将所有参数放入一个数组中,*表示多个
-
栗子:定义一个方法,实现传递1个或者多个Int参数,返回所有参数之和
scala> def m6(x:Int *) = x.sum m6: (x: Int*)Int scala> m6(1) res22: Int = 1 scala> m6(1,2) res23: Int = 3 scala> m6(1,2,4) res24: Int = 7
-
方法的调用形式
-
-
后缀调用法:最常见的最常用的方式
-
语法
对象.方法(参数)
-
栗子
scala> 1.to(10) res25: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1.until(10) res26: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
-
-
中缀调用法/中缀表达式
-
语法
对象 方法 参数
-
栗子
scala> 1 to 10 res27: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res28: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
-
-
花括号调用法
-
语法
对象.方法{参数}
-
栗子
scala> def m7(x:Int) = x + 5 m7: (x: Int)Int scala> m7(1) res29: Int = 6 scala> m7{1} res30: Int = 6
-
注意:只能用于单个参数的方法调用
scala> def m8(a:Int,b:Int) = a + b m8: (a: Int, b: Int)Int scala> m8(1,2) res31: Int = 3 scala> m8{1,2} <console>:1: error: ';' expected but ',' found. m8{1,2}
-
-
函数的定义及语法规则
-
函数的定义及语法
-
语法
(参数) => { //函数体 }
-
栗子1:定义一个函数,传递两个Int参数,返回两个参数的和
scala> (x:Int,y:Int) => { x + y } res32: (Int, Int) => Int = <function2> scala> val str1 = "itcast" str1: String = itcast
-
栗子2:定义一个函数,传递1个Int参数,返回参数值+5
scala> (x:Int) => x + 5 res33: Int => Int = <function1>
-
-
函数的使用规则
- 函数通过=>进行标记:用于区分方法【def】和函数
- 如果函数体只有一行,可以省略{}
- 没有return关键字,使用函数体的最后一行作为返回值
-
函数的本质
-
Scala中函数的本质就是对象,每个函数就是一个FunctionN类的实例,N表示参数个数
-
调用函数的本质是调用了这个对象的方法实现的
-
所以要定义、调用函数,必须指定参数列表,不然只是定义或者调用了这个对象,而不是这个对象的方法
scala> def m9() = 1 + 2 m9: ()Int scala> m9 --调用方法 res35: Int = 3 scala> m9() --调用方法 res36: Int = 3 scala> () => 1 + 2 res37: () => Int = <function0> scala> res37() --调用函数对象的方法 res38: Int = 3 scala> res37 --调用函数的对象 res39: () => Int = <function0>
- 方法如果定义时没有参数,调用时可以不加括号
- 函数不论是否定义了参数,必须加括号
-
函数的分类
-
Scala中函数的分类
- 匿名函数、值函数、高阶函数
-
匿名函数的功能及定义
-
定义:定义一个函数,但不指定函数的名称
-
应用:一般用于作为高阶函数的参数
-
栗子
(x:Int,y:Int) => x + y
-
-
值函数的功能及定义
-
定义:定义一个函数,将这个函数赋值给一个变量或者常量,用于构建函数名称,调用函数
-
应用:一般用于定义函数名称
-
栗子
scala> val f1 = (x:Int,y:Int) => x + y f1: (Int, Int) => Int = <function2> scala> f1(1,2) res43: Int = 3
-
-
高阶函数的功能及定义
-
定义:如果使用函数值作为参数,或者返回值为函数值的“函数”和“方法”,均称之为“高阶函数”。
- 如果一个函数/方法A,它的参数是一个函数B,称A为高阶函数
-
应用:用于更加灵活的实现数据的处理
-
栗子:定义一个函数,用于根据需求实现两个数字的加、减、乘、除
- 传统写法
-
def f2(x:Int,y:Int,flag:Int){
if(flag == 1){
x + y
}else if (flag == 2){
x - y
}
....
}
高阶函数
def f3(p:(Int,Int) => Int):Int = {
p(4,2)
}
val fa = (x:Int,y:Int) => x+y
val fb = (x:Int,y:Int) => x-y
val fc = (x:Int,y:Int) => x*y
val fd = (x:Int,y:Int) => x/y
f3(fa) --使用值函数作为参数
|
f3((x:Int,y:Int) => x+y) --使用匿名函数作为参数
方法转换成函数
- **step1:创建两个方法**
-
高阶函数m1
scala> def m1(f:(Int,Int) => Int) : Int = { | f(4,2) | } m1: (f: (Int, Int) => Int)Int
-
普通方法m2
scala> def m2(a:Int,b:Int) = a + b m2: (a: Int, b: Int)Int
-
step2:将m2由方法转换为函数
scala> val f1 = m2 _ f1: (Int, Int) => Int = <function2>
-
step3:高阶函数中调用f1
scala> m1(f1) res58: Int = 6 scala> m1(m2) --Scala底层实现了隐式转换:自动转换类型 res50: Int = 6