Java教程

函数1

本文主要是介绍函数1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
object han1 {
  def main(args: Array[String]): Unit = {
    //定义一个“二元运算”函数,只操作0.1,具体运算通过参数传入
    def dualfunctionOneandTwo(fun: (Int, Int) => Int): Int = {
      fun(1, 2)
    }
    val add = (a:Int, b:Int) => a + b
    val minus = (a:Int ,b :Int ) => a - b
    println(dualfunctionOneandTwo(add))
    println(dualfunctionOneandTwo(minus))

    //匿名函数简化
    println(dualfunctionOneandTwo((a:Int, b:Int) => a + b))
    println(dualfunctionOneandTwo((a:Int ,b :Int ) => a - b))

    println(dualfunctionOneandTwo((a,b) => a + b ))
    println(dualfunctionOneandTwo(_ + _))
    println {
      dualfunctionOneandTwo(_ - _)

      dualfunctionOneandTwo ((a, b) => a + b)

      dualfunctionOneandTwo ((a, b) => b - a )

      dualfunctionOneandTwo (-_ + _)
    }
  }
}
这篇关于函数1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!