Christian-health / scala-learning

学习scala语言
0 stars 0 forks source link

scala Function1 #13

Open Christian-health opened 5 years ago

Christian-health commented 5 years ago

https://blog.csdn.net/a1158375969/article/details/78748752

在Scala中, 函数也是一种对象, 可以通过某个函数生成函数.

//生成只有一个参数的函数, 输入参数为整形, 结果类型也为整形
val add = new Function1[Int, Int] = {
    def apply(x:Int):Int = {
        x + 1
    }
}

//生成有两个参数的函数, 输入参数为整形, 结果类型也为整形
val add = new Function2[Int, Int, Int] = {
    def apply(x:Int, y:Int):Int = {
        x + 1
    }
}