Scala定义方法,可变参数,默认值,递归

package com.wyh.day01

object ScalaFun1 {
  def main(args: Array[String]): Unit = {
    val result = stringPan("数加")
    println(result)

    val result2 = SumData(1,2,5,6,4,7,8,7)
    println(result2)

    val result3 = Cli("master")
    println(result3)

    val result4 = digui(4)
    println(result4)

  }

  /**
    * 定义个方法
    */
  def stringPan(str:String): String ={
    if("数加".equals(str)){
      "数加"
    }else if("大数据".equals(str)){
      "大数据"
    }else{
      "未匹配到"
    }
  }


  /**
    * 可变参数
    */
  def SumData(data:Int*):Int={
    var sum = 0;
    for (elem <- data) {
      sum += elem

    }
    sum
  }

  /**
    * 函数的默认值
    */
  def Cli(ip:String,post:Int = 6379): String ={
    ip+"\t"+post

  }

  /**
    *
    * 函数的递归
    */
  def digui(long: Long):Long = {
    if(long==1L){
     return 1
    }else{
      long*digui(long-1)
    }
  }

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