【Scala】Scala中指定参数类型为函数的方法 -- 高阶函数

高阶函数的基本构成

def 方法名(函数名: (函数的参数类型) => 函数的返回值类型) {
    处理的具体内容
  }  

案例

package com.spark.demo

object HigherOrderFunction {

  def main(args: Array[String]): Unit = {

    // 定义函数为变量
    val fuction1 = (str: String) => {
      println(str)
    }

    // 定义函数为变量2
    val fuction2 = (str: String) => {
      println("Wecome to Scala's " + str)
    }

    // 设定方法的参数为函数
    /**
      * World
      */
    execute(fuction1)

    /**
      * Wecome to Scala's World
      */
    execute(fuction2)

  }

  // 定义方法的参数为函数
  def execute(function: (String) => Unit) {
    function("World")
  }

}
posted @ 2020-09-18 16:47  初入门径  阅读(511)  评论(0)    收藏  举报