Scala匿名函数

package com.wyh.day01

/**
  * 面向函数编程
  */
object ScalaFun2 {
  def main(args: Array[String]): Unit = {

    val result = A(f)
    println(result)

    val result2 = A(f1)
    println(result2)

    var result3 = A(f2)
    println(result3)

    /**
      * 匿名函数,只需写名参数类型,主体方法,当主体方法只有一行的时候,大括号可以省去
       */
    println(A((str:String) => str+"匿名函数1"))
    println(A(str => str+"匿名函数2"))

  }

  def A(f:String => String):String={
    val result = f("Java")

    result
  }

  def f(str:String):String={
    "前缀"+str

  }

  def f1(str:String):String={
    str+"后缀"
  }

  def f2(str:String):String={
    str+"复习"

  }



}
posted @ 2020-01-20 12:54  Xiaohu_BigData  阅读(216)  评论(0编辑  收藏  举报