scala学习之函数(function)

由于学习spark,所以也不得不学习scala,其实网上关于spark的学习方面的东西还是很少的,今天就关于scala语言的学习提供一些自己的东西,贡献一下自己力量,不多说,这一篇是关于函数的介绍,直接上代码:

class Hello {
 
}

object Hello{
 
    def hello(name:String):String={
    
            "hello : "+name
      
    }
 
  //带默认值得函数
  def hello1(name:String="first scala"):String={
    "hello : "+name
  }
 
  def helloscala(){
    println("hello scala!!")
  }
 
  //匿名函数
  def add=(x:Int,y:Int)=>x+y
 
  //颗粒化
  def add1(x:Int)(y:Int)=x+y
 
  var add2=(x:Int,y:Int)=>x+y
 
  def printEveryChar(c:String*){
    c.foreach { x =>println(x)  }
  }
 
  def main(args:Array[String]){
    
    println("hello scala")
    
    println(hello("ymt"))
    
    println(hello1())
    
    helloscala
    
    println(add(1,2))
    
    println(add1(9)(10))
    
    println(add2(3,4))
    
    printEveryChar("a","b","c","d","e")

  }

}

后记,如果你真的是想学习,请你把上面的代码自己直接敲一遍,然后运行一下,仔细揣度下里面的东西

posted @ 2015-09-07 08:35  杨MT  阅读(275)  评论(0编辑  收藏  举报