scala 系列之 04scala 方法和函数

教程 潘牛 ⋅ 于 2021-06-10 19:54:01 ⋅ 1133 阅读

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()
<console>: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 = <function2>
scala> res10(4,5)
res11: Int = 9
// 把匿名函数赋给变量,这个变量名称就成了函数名称
scala> val add:(Int,Int)=>Int = (a:Int, b:Int) => a + b
add: (Int, Int) => Int = <function2>
scala> add(4,5)
res12: Int = 9

function中的参数最多有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://hainiubl.com/topics/75675
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter