Scala中特殊函数的使用(代码)

这里写的是一些简洁的案例来对这些特殊函数的理解以及解释

object FunDemo2 {
  //sayHello("老王","你好");
  /* 调用一个函数的时候,可以指定 参数的名称(必须和函数定义的名称一致)
  *  不需要安装顺序指定数值
  *   sayHello(msg = "你好",name = "LT")
  *
  */
  def sayHello(name: String,msg:String) = println(s"$name,----$msg")

  /**
    * 定义函数的时候,根据给函数的参数赋值,称为参数的默认值
    * 注意:参数有默认值的话,参数必须放在参数列表最后面
    * @param name
    * @param msg
    */
  def sayHello1(name:String,msg:String="Hi..")=println(s"$name,----$msg")

  def f1():Unit ={
    println("f1() invoke")
    //定义内部函数
    def g():Unit = {
      println("g1() invoke")
    }
    //调用内部函数
    g()
  }

  //参数可变,Java 5 中也提供可变长度参数
  def printCourses(course:String *):Unit = {
    course.foreach(item=>println(item))
    course.foreach(println)
  }

  def main(args: Array[String]): Unit = {
    sayHello("LL","姥姥")
    printCourses("hadoop","hive")

    val xx = Array("hadoop","hive","spark")
    printCourses(xx:_*)

    //def foreach[U](f:A=>U):Unit ={}
    xx.foreach((item)=>{

    })
  }

}

猜你喜欢

转载自blog.csdn.net/wyz0516071128/article/details/81039661