scala 系列之 04scala 方法和函数 |
您所在的位置:网站首页 › integer函数怎么用 › scala 系列之 04scala 方法和函数 |
5 方法与函数
5.1 方法
在scala中的操作符都被当成方法存在,比如说+、-、*、/ 1+2就是1.+(2)的调用, 2.0 是doule类型,强调用Int类型的写法为1.+(2:Int) file可以在idea中搜索Int类查看支持的方法 file方法声明与使用 定义方法的语法: def 方法名([变量:变量类型,变量:变量类型]):返回值类型={方法体}其中: 在scala 中,方法里面的最后一个表达式的值就是方法的返回值,不需要return 返回;示例: 定义无参无返回值的方法: // 定义无参无返回值的方法 scala> def say():Unit = {println("say hello")} say: ()Unit scala> say() say hello // 简化过程 scala> def say():Unit = {println("say hello")} say: ()Unit // 方法体有一个语句,省略{} scala> def say():Unit = println("say hello") say: ()Unit // 方法返回值可以由方法体返回结果类型推测 scala> def say() = println("say hello") say: ()Unit // 方法形参列表是空, 可省略() scala> def say = println("say hello") say: Unit scala> say say hello scala> say() :13: error: Unit does not take parameters say() // 带有返回值的方法 def add(a:Int, b:Int):Int={val c = a + b; return c}定义带有有参有返回值方法: // 定义带有有参有返回值方法 scala> def add(a:Int, b:Int):Int={val c = a + b; return c} add: (a: Int, b: Int)Int scala> add(4,5) res8: Int = 9 // 简化流程 scala> def add(a:Int, b:Int):Int={val c = a + b; return c} add: (a: Int, b: Int)Int // scala 不建议用return返回方法结果,默认最后一个就是方法的返回值 scala> def add(a:Int, b:Int):Int={val c = a + b; c} add: (a: Int, b: Int)Int // 去掉中间变量c scala> def add(a:Int, b:Int):Int={a + b} add: (a: Int, b: Int)Int // 方法体有一个语句,省略{} scala> def add(a:Int, b:Int):Int=a + b add: (a: Int, b: Int)Int // 方法返回值可以由方法体返回结果类型推测 scala> def add(a:Int, b:Int)=a + b add: (a: Int, b: Int)Int scala> add(4,5) res9: Int = 9方法的调用: object M1 { def say(name:String) = { println(s"say ${name}") } def add(a:Int, b:Int) = a + b def main(args: Array[String]): Unit = { // 普通调用 M1.say("hainiu") // 中缀方法调用 M1 say "hainiu" // 大括号调用,当只有一个入参时才能用 M1 say {"hainiu"} M1.add(4,5) // 中缀方法调用 M1 add (4,5) } } 5.2 函数 在 java 中方法和函数是一个意思,在 scala 中方法和函数是两种含义。 在 scala 中,函数是一等公民。可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值;函数还可以赋给变量。函数声明: val 变量名:[变量类型1,变量类型2 => 函数体返回类型 ] = ([变量:变量类型,变量:变量类型]) => 函数体示例: // 函数本身是没有名字的--匿名函数 // function2 是 函数有 两个输入参数 和 一个输出, 本例是 两个Int输入,一个Int输出 scala> (a:Int, b:Int) => a + b res10: (Int, Int) => Int = scala> res10(4,5) res11: Int = 9 // 把匿名函数赋给变量,这个变量名称就成了函数名称 scala> val add:(Int,Int)=>Int = (a:Int, b:Int) => a + b add: (Int, Int) => Int = scala> add(4,5) res12: Int = 9function中的参数最多有22个 file函数的结果做为方法的参数: 示例: // 定义周长函数 val perimeter = (a:Int,b:Int) => (a+b) *2 // 定义面积函数 val area = (a:Int, b:Int) => a*b // 定义求和方法 def add(a:Int, b:Int) = a+b // 计算长方形周长和面积的和 println(add(perimeter(4,5), area(4,5)))函数作为方法的参数: // 定义js方法,内部有个入参是函数 scala> def js(a:Int, b:Int, func:(Int,Int)=>Int) = func(a,b) js: (a: Int, b: Int, func: (Int, Int) => Int)Int // 调用时,只要符合两个Int输入,一个Int输出的函数都可以作为参数 scala> js(4,5, perimeter) // 计算周长 res13: Int = 18 scala> js(4,5, area) // 计算面积 res14: Int = 20方法转换成函数 1)用空格下划线的方式 # 定义方法 def add_def(a:Int,b:Int) = a + b # 方法转函数,用空格下划线的方式 val add_func = add_def_2)也可以把方法当参数使用,这也因为scala会隐式的把方法转换成函数,但并不是直接支持方法当参数的模式,只是做了隐式的转换,这种函数的转换分两种显示用_和隐式的,这也体现了scala灵活的地方。 # 定义方法add_def def add_def(a:Int,b:Int) = a + b # 定义方法js,接收参数是函数 def js(a:Int, b:Int, func:(Int,Int) => Int) = func(a,b) # 隐式的将方法转换成了函数 println(js(add_def))海汼部落原创文章,原文链接:http://www.hainiubl.com/topics/75675 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |