📌  相关文章
📜  如何在 Kotlin 中将函数作为参数传递给另一个函数?

📅  最后修改于: 2022-05-13 01:54:25.262000             🧑  作者: Mango

如何在 Kotlin 中将函数作为参数传递给另一个函数?

Kotlin 赋予我们声明高阶函数的能力。在高阶函数中,我们可以将函数作为参数传递和返回。这是一个非常有用的功能,使我们的代码更易于使用。事实上,Kotlin 库的很多函数都是高阶的,比如 NBQ。在 Kotlin 中,我们可以将函数和函数引用声明为值,然后传递给函数。我们将首先了解如何声明 lambda,然后了解如何将它们传递给函数。

例子

让我们首先了解我们如何将函数声明为 lambda:

Kotlin
fun main (args: Array) {
  val funcMultiply - {a: Int, b: Int -> a*b}
  println (funcMultiply (4, 3))
  val funcSayHi - {name: String -> println ("Hi $name") }
  funcSayHi("John")
}


Kotlin
fun main (args: Array) {
  val funcMultiply : (Int, Int)->Int = {a: Int, b:Int -> a*b}
  println (funcMultiply (4, 3))
  val funcSayHi : (String) ->Unit = {name: String -> println("Hi $name")}
  funcSayHi ("John")
}


Kotlin
fun main (args: Array) { 
  val funcMultiply : (Int, Int) ->Int = {a: Int, b:Int -> a*b}
  val funcSum : (Int, Int) ->Int = {a: Int, b: Int -> a+b}
  performMath (3, 4, funcMultiply)
  performMath (3, 4, funcSum)
}
  
fun performMath (a: Int, b:Int, mathFunc : (Int, Int) -> Int) : Unit
{
  println ("Value of calculation: ${mathFunc (a, b) }")
}


Kotlin
fun main(args: Array) {
  // free delivery of order above 499
  val productPricel = 600; 
  // not eligible for free deliver
  val productPrice2 = 300; 
  val totalCost1 = totalCost (productPrice1)
  val totalCost2 = totalCost (productPrice2)
    
  println("Total cost for item 1 is ${totalCost1 (productPrice1) }")
  println ("Total cost for item 2 is ${totalCost2 (productPrice2) }")
}
  
fun totalCost (productCost : Int) : (Int) -> Int(
  if (productCost > 499) {
    return { x -> x }
  }
  else {
    return { x -> x + 50 }
  }
}


在前面的代码块中,我们声明了两个 lambda:一个 (funcMultiply) 接受两个整数并返回一个整数,另一个 (funcSayHi) lambda 接受一个字符串并返回一个单位——也就是说,它什么也不返回。尽管在前面的示例中我们不需要声明参数类型和返回类型,但在某些情况下我们需要显式声明参数类型和返回类型。我们通过以下方式做到这一点:

科特林

fun main (args: Array) {
  val funcMultiply : (Int, Int)->Int = {a: Int, b:Int -> a*b}
  println (funcMultiply (4, 3))
  val funcSayHi : (String) ->Unit = {name: String -> println("Hi $name")}
  funcSayHi ("John")
}

所以现在我们对 lambdas 的工作原理有了一个大致的了解,让我们尝试将一个传递给另一个函数——也就是说,我们将尝试一个高阶函数。查看此代码段:

科特林

fun main (args: Array) { 
  val funcMultiply : (Int, Int) ->Int = {a: Int, b:Int -> a*b}
  val funcSum : (Int, Int) ->Int = {a: Int, b: Int -> a+b}
  performMath (3, 4, funcMultiply)
  performMath (3, 4, funcSum)
}
  
fun performMath (a: Int, b:Int, mathFunc : (Int, Int) -> Int) : Unit
{
  println ("Value of calculation: ${mathFunc (a, b) }")
}

就这么简单——创建一个函数lambda 并将其传递给函数。所以这只是高阶函数的一个方面——也就是说,我们可以将函数作为参数传递给函数。高阶函数的另一个用途是返回一个函数。考虑以下示例,其中我们需要一个根据特定条件转换订单总价的函数。有点像电子商务网站,但更简单

科特林

fun main(args: Array) {
  // free delivery of order above 499
  val productPricel = 600; 
  // not eligible for free deliver
  val productPrice2 = 300; 
  val totalCost1 = totalCost (productPrice1)
  val totalCost2 = totalCost (productPrice2)
    
  println("Total cost for item 1 is ${totalCost1 (productPrice1) }")
  println ("Total cost for item 2 is ${totalCost2 (productPrice2) }")
}
  
fun totalCost (productCost : Int) : (Int) -> Int(
  if (productCost > 499) {
    return { x -> x }
  }
  else {
    return { x -> x + 50 }
  }
}

请注意我们需要如何根据某些条件更改我们应用的函数,以便我们返回一个适合这些条件的函数。我们将返回的函数分配给一个变量,然后我们可以将 append() 放在变量前面以将其用作函数,就像我们对 lambdas 所做的那样。这是有效的,因为高阶函数本质上是返回一个 lambda。

在 Kotlin 中,我们可以将一个函数分配给一个变量,然后我们可以将它传递给一个函数或从一个函数中返回它。这是因为它本质上是像变量一样声明的。这是使用函数的 lambda 声明来完成的。