函数式接口:
一种只含有一个抽象方法声明的接口可以使用匿名内部类实例化函数式接口的对象通过Lambda表达式可以进一步简化代码Lambda语法:
(parameters) -> expression (parameters) -> { statements; }实例:
def square(x:Int):Int={ println(x) x * x } def sayHello(x:String):Unit={ println("Hello "+x) }传值调用 call-by-value 传值调用时,参数只在调用时计算一次,后续重复使用计算结果
def square(x:Int):Int={ println(x) //3 x * x } square(1+2) // 先计算1+2传名调用 call-by-name 传名调用时,参数在调用时不会计算,只有真正用到参数时才计算
def square(x:=>Int):Int={ println(x) //计算1+2 x * x //计算(1+2)*(1+2) } square(1+2) //不计算通常情况下,传入参数与函数定义的参数列表一一对应,而命名参数允许使用任意顺序传入参数
def showMsg(name:String,age:Int)={ println("Hello "+name+" age:"+age) } showMsg("kb09",3) showMsg(age=18,name="zhangsan")Scala函数允许指定参数的缺省值,从而允许在调用函数时不指明该参数
def printName(first:String="John",last:String="Smith")={ println(first+" "+last) } printName() //输出"John Smith"匿名函数指不含函数名称的函数
匿名函数的定义:
(参数列表)=>{函数体} //如果函数体包含多条语句,应使用{}包含匿名函数调用实例:
(a:Int, b:Int)=>a+b //用变量去接受匿名函数 val aa=(a:Int, b:Int)=>a+b val bb=aa println(aa(1,3)) //输出4 println(bb(3,5)) //输出8输出一个指定数的阶乘(递归思路)
def jc(a:Int)={ if (a==1) 1 else a*jc(a-1) } println(jc(8)) //40320方法可以定义多个参数列表,当使用较少的参数列表调用多参数列表的方法时,会产生一个新的函数,该函数接收剩余的参数列表作为其参数。这被称为柯里化
//单参数列表 def modN(n: Int,x: Int) = ((x % n) == 0) //多参数列表 def modN(n: Int)(x: Int) = ((x % n) == 0) //新函数接收剩余的参数列表作为其参数 def f1(x: Int) = modN(10)(x) def f2(n: Int) = modN(n)(10) def f3 = modN(10)(_) def fun(a:Int,b:Int,c:Int,d:Int)=a+b+c+d def fun1(a:Int,b:Int)(c:Int,d:Int)=a+b+c+d def fun2(a:Int)(b:Int)(c:Int)(d:Int)=a+b+c+d println(fun2(1)(2)(3)(4))方法可以具有隐式参数列表,由参数列表开头的implicit关键字标记
implict只能修改最尾部的参数列表,应用于其全部参数Scala可自动传递正确类型的隐式值通常与柯里化函数结合使用 //隐式参数 implicit var a:Int=10 implicit var str:String="hello" def fun(a:Int,b:Int)(implicit c:Int):Int={ a+b+c } def fun1(a:Int,b:Int)(implicit c:Int=5,str:String):Int={ println(str) a+b+c } //优先级:传参>隐式参数>默认 println(fun1(1, 2)(str="abc"))隐式函数也称隐式转换,使用implicit修饰的函数
类型转换 implicit def double2Int(x:Double)=x.toInt val i:Int=3.5 类型增强 implicit def bool2Int(x:Boolean)=if(x) 1 else 0 println(1+true)更多隐式实例:
package Date1022 import Date1022.implicitpack._ class demo2{} object demo2 { def main(args: Array[String]): Unit = { // val res1 = sum(10,10) // println(res1) // val res2 = showMsg("张三") // println(res2) // val res3:Int=3.5 // println(res3) // val res4:Int="20" // println(res4) val dm =new demo2 println(dm.multi(4, 6)) println(dm.sum(10, 90)) println(dm.divide(30, 4)) } def sum(a:Int,b:Int)(implicit c:Int):Int=a+b+c def showMsg(name:String)(implicit s:String):String=s+name //def multi(a:Int,b:Int)(implicit c:Int):Int=a*b*c }隐式类
package Date1022 class demo3{} import Date1022.implicitpack._ object demo3 { //隐式类的使用,与demo3相关的两个隐式类全部在Implicitpack中 //创建好demo3对象中,虽然demo3中没有一个方法,但是因为隐式 //类中是有好多方法的,因些demo3对象后就可以使用这些方法 //隐式类的主要作用就是类对象中没有的方法可以直接被类对象调用 def main(args: Array[String]): Unit = { val dm3=new demo3 dm3.aa("k") println(dm3.bb(19)) println(dm3.add(2, 5)) println(dm3.aa("zhangsan", "123")) } }隐式包
package Date1022 object implicitpack { implicit val a:Int=10 implicit val b:String="b" implicit def doubletoint(value:Double): Int =value.toInt implicit def stringtoint(value:String): Int =Integer.parseInt(value) implicit class NumOperation(demo:demo2){ def multi(a:Int,b:Int)={ a*b } def sum(a:Int,b:Int)={ a+b } def sub(a:Int,b:Int)={ a-b } def divide(a:Int,b:Int)={ a/b } } implicit class demo3_implicit(demo:demo3){ def aa(a:String): Unit ={ println(a+a) } def bb(num:Int): String ={ if (num%2==0) "偶数" else "奇数" } def add(a:Int, b:Int): Int ={ a+b } } implicit class demo3_implicit2(demo:demo3){ def aa(name:String,pwd:String):Boolean={ if (name=="张三" && pwd=="123456") true else false } } }