Kotlin概念----函數(shù)

函數(shù)

在kotlin中 使用fun關(guān)鍵字聲明一個(gè)函數(shù)

fun double(x:Int):Int{
  return 2*x;
}

函數(shù)參數(shù)

有默認(rèn)值的函數(shù),可減少重載

fun read(
  b:ByteArray,
  off:Int =0,
  len:Int = b.size
){}

覆寫具有默認(rèn)值的函數(shù)時(shí),默認(rèn)參數(shù)的值一定要從方法簽名中省略

open class A {
    open fun foo(i: Int = 10) { /*...*/ }
}
class B : A() {
    override fun foo(i: Int) { /*...*/ }  // No default value is allowed.
}

如果一個(gè)默認(rèn)參數(shù)在非默認(rèn)參數(shù)的前面,需要在調(diào)用方法傳入?yún)?shù)時(shí)指定參數(shù)的名字

fun foo(
    bar: Int = 0,
    baz: Int,
) { /*...*/ }

foo(baz = 1) // The default value bar = 0 is used

如果在最后一個(gè)默認(rèn)參數(shù)后面是一個(gè)lambda,可以通過使用命名參數(shù)或者括號(hào)外

fun foo(
    bar: Int = 0,
    baz: Int = 1,
    qux: () -> Unit,
) { /*...*/ }

foo(1) { println("hello") }     // Uses the default value baz = 1
foo(qux = { println("hello") }) // Uses both default values bar = 0 and baz = 1
foo { println("hello") }        // Uses both default values bar = 0 and baz = 1
//輸出
hello a
bar 1 , baz 1
hello b
bar 0 , baz 1
hello c
bar 0 , baz 1

返回Unit函數(shù)

Unit代表返回一個(gè)沒有用的值,不必顯示返回

fun printHello(name: String?): Unit {
    if (name != null)
        println("Hello $name")
    else
        println("Hi there!")
    // `return Unit` or `return` is optional
}

可變參數(shù)Varargs

中綴表達(dá)式Infix notation

中綴表達(dá)式必須滿足下列的條件

  • 必須是成員函數(shù)或者是擴(kuò)展函數(shù)
  • 必須只有一個(gè)參數(shù)
  • 參數(shù)不能是可變參數(shù),并且沒有默認(rèn)值
infix fun Int.shl(x: Int): Int { ... }

// calling the function using the infix notation
1 shl 2

// is the same as
1.shl(2)

中綴表達(dá)式的優(yōu)先級(jí)要低于算數(shù)表達(dá)式 ,但高于&& || is in操作符

本地函數(shù),在函數(shù)內(nèi)部定義的函數(shù)

fun dfs(graph: Graph) {
    fun dfs(current: Vertex, visited: MutableSet<Vertex>) {
        if (!visited.add(current)) return
        for (v in current.neighbors)
            dfs(v, visited)
    }

    dfs(graph.vertices[0], HashSet())
}

范型函數(shù)

在函數(shù)名前,用一個(gè)尖括號(hào)表識(shí)范型

fun <T> singletonList(item: T): List<T> { /*...*/ }

尾遞歸函數(shù)

對(duì)于一些循環(huán)函數(shù),可以使用遞歸來代替,并且沒有stack overflow風(fēng)險(xiǎn)。當(dāng)一個(gè)函數(shù)被標(biāo)識(shí)為tailrec ,編譯器會(huì)進(jìn)行優(yōu)化,以達(dá)到更搞得效率。
注意不能在try/catch/finally中調(diào)用。

val eps = 1E-10 // "good enough", could be 10^-15

tailrec fun findFixPoint(x: Double = 1.0): Double =
    if (Math.abs(x - Math.cos(x)) < eps) x else findFixPoint(Math.cos(x))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 函數(shù)聲明 Kotlin 中的函數(shù)使用 fun 關(guān)鍵字聲明: 函數(shù)用法 調(diào)用函數(shù)使用傳統(tǒng)的方法: 默認(rèn)參數(shù) 函數(shù)參數(shù)...
    有腹肌的豌豆Z閱讀 1,116評(píng)論 0 1
  • kotlin函數(shù) 本文主要介紹Kotlin函數(shù)的基礎(chǔ)使用和常見用法,大部分內(nèi)容來自官方文檔,也包含個(gè)人理解內(nèi)容,將...
    小神之路閱讀 642評(píng)論 0 0
  • 更多文章可以訪問我的博客Aengus | Blog 函數(shù)Function Kotlin中的函數(shù)相較于Java增加了...
    Aengus_Sun閱讀 528評(píng)論 0 1
  • title: Kotlin(函數(shù))top: falsedate: 2019-07-03 17:56:30tags:...
    無語_ae67閱讀 217評(píng)論 0 0
  • 寫在開頭:本人打算開始寫一個(gè)Kotlin系列的教程,一是使自己記憶和理解的更加深刻,二是可以分享給同樣想學(xué)習(xí)Kot...
    胡奚冰閱讀 734評(píng)論 0 4

友情鏈接更多精彩內(nèi)容