Scala控制抽象(值调用和名调用)

值调用

  • 将函数的值作为参数传递。
def f1(x: Int): Unit ={
    
    
  println(x)
  println(x)
}

def f2(): Int = {
    
    
  10
}

// 值调用
f1(f2)  // 10   10

名调用

  • 将函数的代码块作为参数传递。
  • 格式:=> 返回值类型
 def f3(x: => Int): Unit = {
    
    
  println(x)
  println(x)
}

def f4(): Int = {
    
    
  println("f4...")
  10
}

// 名调用  f3函数中执行了两次代码块
f3(f4)    // f4 10      f4 10

猜你喜欢

转载自blog.csdn.net/FlatTiger/article/details/114401084