Kotlin-21.関数(Funtions)

4414 ワード

公式文書:http://kotlinlang.org/docs/reference/functions.html
1.関数定義と呼び出し
  java, kotlin    /        
1.      /  (Function Declarations):
    //Unit         
    fun demo(): Unit {
        ...
        // return Unit   return                          
    }

    //    Unit
    fun demo() {
        ...
        // return Unit   return                          
    }

2.        /  :
    //           ,      {},           :
    fun double(x: Int): Int = x * 2
    
    //              ,       :
    fun double(x: Int) = x * 2
1.従来の呼び出し
      :
    demo()

      :
    class Sample() {
        fun foo() { print("Foo") }
    }
    Sample().foo()
2.中缀呼び出し(Infix notation)
        [    ]    ,         (+ - * /)
        :
                     
               
          infix   

//     -    /  
infix fun Int.add(i: Int): Int{
    return this + i
} 

//     -    /  
class MyInt(var a: Int){    
    infix fun add(i: Int): Int{
        return a + i
    }
}

fun main(args: Array) {          
    println(2 add 3)  //    ,  5       
    println(2.add(3)) //    ,  5

    val i = MyInt(2)
    println(i add 3)  //    ,  5
    println(i.add(3)) //    ,  5
}
2.関数パラメータ
     Pascal     ( name: type),       ,          :
    fun myFun(p1: Int, p2: Int) {
    }
1.デフォルトパラメータ(Default Agments)
          ,          ,      :
    fun myFun(p1: Int = 1, p2: Int = 2) {
    }
 
         (  )         ,       ,         :
    open class A {
        open fun foo(i: Int = 10) {            
        }
    }

    class B : A() {
        //     ,          
        override fun foo(i: Int) {      
        }
    }
2.命名パラメータ(Named Agments)
                 :
    fun myFun(a: String = "lioil", 
              b: Boolean = true, 
              c: Int = 1, 
              d: Char = 'w') {        
    }
    //    c,        ,       ,      !!!
    myFun("lioil",true,2)        
    //        (Named Arguments)  :
    myFun(c = 2)
3.パラメータ個数可変(vararg)
     vararg  ,        :
    fun  asList(vararg ts: T){        
        for (t in ts) //  ts     Array 
            print("$t,")
    }

    fun main(args: Array) {
        asList(1,2,3) //  1,2,3,

        //    ,    (spread)   *
        val a = arrayOf(1, 2, 3)
        asList(0, *a, 4) //  0,1,2,3,4,
    }

  vararg             ,   [    ]        :        
    fun  asList(vararg ts: T, name: String){  
        for (t in ts)
            print("$t,")
        print(name)
    }

    fun main(args: Array) {            
        asList(1,2,3,name="lioil.win")//  1,2,3,lioil.win
    }
3.関数の種類
Kotlin          /  ,   Java、C#、Scala             ,
      kotlin       !
  ,Kotlin            ,         ,           !

1.  /    (Local Functions)-       
    Kotlin      ,             :    
    fun main(args: Array) {
        val name = "lioil.win"
        fun myFun() {
        //           (   )     name
            println(name)
        } 
        myFun() //   lioil.win
    }

2.    (Member Functions)             
    class Sample() {
        fun foo() { print("Foo") }
    }
    //      
    Sample().foo()

3.    (Generic Functions),          <>    
    fun  singletonList(item: T): List {
    }

4.     (Tail recursive functions)
    Kotlin     (tail recursion)  :             ,          !
      tailrec       ,                 !
      tailrec      :
                         (       )
             try/catch/finally   
                 JVM      

    //     
    tailrec fun findFixPoint(x: Double = 1.0): Double
            = if (x == Math.cos(x)) x else findFixPoint(Math.cos(x))
    
    //             
    private fun findFixPoint(): Double {
        var x = 1.0
        while (true) {
            val y = Math.cos(x)
            if (x == y) return y
            x = y
        }
    }

5.    (Inline Functions)、    (Extension Functions)、
    (Higher-Order Functions) Lambda          ...
:http://www.jianshu.com/p/c3146fa3261a CSDNブログ:http://blog.csdn.net/qq_32115349/articale/details/73825015 GitHubブログ:http://lioil.win/2017/06/27/Kotlin-functions.html Codingブログ:http://c.lioil.win/2017/06/27/Kotlin-functions.html