Scala in functions and methods (finally became known)

Is a set of functions to perform a task with the statement. You can put the code into different functions. How to divide the code into separate functions it is up to you to decide, but logically divided usually carried out each function to perform a specific task based on.

Scala has functions and methods, the difference between the two semantically small. Scala method is part of the class, and is a function of the object can be assigned to a variable. In other words, the function defined in a class that is a method.

We can define functions anywhere, even defined functions (nested functions) within a function. More important point is that Scala function name can have the following special characters: +, +, -, &, -, -, /,:, etc.

Statement format function:
DEF functionName ([parameter list]): [return type]
case:

object Function {
 
  def main(args: Array[String]): Unit = {
    println(fun01(10)) //函数的调用
    println(fun02(10, 20)) //函数的调用
 
    //函数作为参数传给方法:
    val res = method02(10, fun02)
    println(res)
 
  }
 
  //函数的定义:val/var 函数名称=(函数的参数列表)=>函数体
  val fun01 = (a: Int) => a + 10
 
  //定义函数,有两个Int类型的参数
  val fun02 = (a: Int, b: Int) => {
    if (a > 10 || b > 10) a + b else a - b
  }
 
 
  //  方法和函数的区别:
  //  在函数式编程语言中,函数是“头等公民”,它可以像任何其他数据类型
  // 一样被传递和操
 
 
  //定义一个求和的函数
  val fun_sum = (x: Int, y: Int) => x + y
 
 
  //函数f的参数个数为1,函数f的参数的数据类型为Int,函数f的返回值是Int
  def method00(a: Int, f: Int => Int) = {
    a + f(10)
  }
 
 
  def method00(a: Int, b: Int, c: Int) = {
    //调用函数f对10和5求和之后,再和参数a求和
    a + b + c
  }
 
  //函数f的参数个数为2,函数f的参数的数据类型都为Int,函数f的返回值是Int
  def method02(a: Int, f: (Int, Int) => Int) = {
    //调用函数f对10和5求和之后,再和参数a求和
    var x = 10
    var y = 5
    val fvalues = f(x, y) //调用函数实现对10与5的和
    a + fvalues
  }
 
 
}

 /**

  *  方法的声明和使用

  */

  

	
object Method {
 
  def main(args: Array[String]) {
    //调用:method01()或者method01
    //    method01
    //    method03
    println(method05("method05", 20))
  }
 
 
  /**
    * 使用def定义一个方法
    * 方法的定义:def 方法名(参数列表):返回值={方法体}
    */
  //def method07= 10
 
  def method01(): Unit = println("this is my first method")
 
 
  /**
    * 如果没有参数、可以直接不用写()
    */
  def method02 = println("this is my first method")
 
 
  /**
    * 如果没有参数、不确定返回值数据类型时
    */
  def method03 = {
    println("this is my first method")
  }
 
  /**
    * 指定方法的返回数据类型,方法的最后一个表达式作为方法的返回值
    */
  def method04: String = {
    "this is my first method"
  }
 
  /**
    * 当方法需要传入参数是时:
    *
    * @return
    */
  def method05(str: String, age: Int): String = {
    //把参数传入字符串里
    s"$str method,and age=$age"
  }
 
}

// 方法的参数列表  常用的参数有默认参数,带名参数和可变参数

	
object MethodArgment {
 
  def main(args: Array[String]) {
    //调用默认参数:
    method01("xingyue", 20, "nan")
    //  或者
    method01("qingyuan",22 )
 
    println("-----------------------------")
 
    //正常传递参数,根据参数列表顺序传递
    method02("laozhang", 40)
    //scala的带名参数:
    method02(age = 40, name = "laozhang")
 
    println("----------可变参数-------------------")
    method03(10, 20, 30, 40, 100, 1000)
 
  }
 
  /**
    * 默认参数
    * //我们在调用方法的时候,在某些情况下,没给参数,这个时候,
    * 方法会使用默认的一个参数,但是这个参数从哪里来?? 我在定义方法的时候就给了这个默认参数
    * //当我们给的参数不够的时候,会自动的依次补齐默认参数
    *
    * @param name
    * @param age
    * @param sex
    */
  def method01(name: String, age: Int, sex: String = "nan"): Unit = {
    val info = s"your name is $name,and age is $age,and sex is $sex"
    println(info)
 
  }
 
  /**
    * 带名参数:在调用函数的时候,我们不按照函数的定义参数列表来传递参数,
    * 使用的是带名参数的方式来传递
    *
    * @param name
    * @param age
    */
  def method02(name: String, age: Int): Unit = {
    val info = s"your name is $name,and age is $age"
    println(info)
 
  }
 
 
  /**
    * 可变参数:在定义函数时,可能无法确认出到底有多少个参数,可以用变长参数的形式来定义函数
    *
    * @param argInt
    */
  def method03(argInt: Int*): Unit = {
 
    //变量可变参数遍历方式1
    for (arg <- argInt.iterator)
      println(arg)
 
    //变量可变参数遍历方式2
    for (arg <- argInt)
      println(arg)
 
  }
 
  def method04(age: Int, str: String*): Unit = {
 
    //变量可变参数遍历方式1
    for (arg <- str.iterator)
      println(age + ":" + arg)
 
    //变量可变参数遍历方式2
    for (st <- str)
      println(age + ":" + st)
 
  }
 
}
Published 27 original articles · won praise 9 · views 20000 +

Guess you like

Origin blog.csdn.net/I_Demo/article/details/102921608