=>

使用场景

package graphx

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

    //TODO 1.表示函数的返回类型


    def double(x:Int):Int=x*2

    //定义一个函数变量
    var x:(Int)=>Int=double
    println(x(2))


    //TODO 匿名函数 左边是参数 右边是函数实现体 (x: Int)=>{}
    var xx = (x:Int)=> x+1



    //TODO case语句
    val booleans = List(true,false)
    for(bool <- booleans){
      bool match{
        case true=>println("heads")
        case false=>println("tails")
        case _=>println("no")
      }
    }

    //TODO By-Name Parameters(传名参数),传名参数在函数调用前表达式不会被求值,而是会被包裹成一个匿名函数作为函数参数传递下去,
    def doubles(x: =>Int)={
      println(x)
      x*2
    }

    println(doubles(3))

    def f(x:Int):Int={
      x
    }
    println("********************")
    println(doubles(f(3)))






  }



}

 

posted on 2020-10-27 12:03  happygril3  阅读(127)  评论(0)    收藏  举报

导航