美文网首页
Kotlin概念----函数

Kotlin概念----函数

作者: Boahui | 来源:发表于2021-03-31 06:48 被阅读0次

函数

在kotlin中 使用fun关键字声明一个函数

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

函数参数

有默认值的函数,可减少重载

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

覆写具有默认值的函数时,默认参数的值一定要从方法签名中省略

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

如果一个默认参数在非默认参数的前面,需要在调用方法传入参数时指定参数的名字

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

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

如果在最后一个默认参数后面是一个lambda,可以通过使用命名参数或者括号外

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函数

Unit代表返回一个没有用的值,不必显示返回

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

可变参数Varargs

中缀表达式Infix notation

中缀表达式必须满足下列的条件

  • 必须是成员函数或者是扩展函数
  • 必须只有一个参数
  • 参数不能是可变参数,并且没有默认值
infix fun Int.shl(x: Int): Int { ... }

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

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

中缀表达式的优先级要低于算数表达式 ,但高于&& || is in操作符

本地函数,在函数内部定义的函数

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())
}

范型函数

在函数名前,用一个尖括号表识范型

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

尾递归函数

对于一些循环函数,可以使用递归来代替,并且没有stack overflow风险。当一个函数被标识为tailrec ,编译器会进行优化,以达到更搞得效率。
注意不能在try/catch/finally中调用。

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))

相关文章

  • Kotlin概念----函数

    函数 在kotlin中 使用fun关键字声明一个函数 函数参数 有默认值的函数,可减少重载 覆写具有默认值的函数时...

  • Kotlin中init方法调用时机笔记

    前提概念:kotlin中主级函数、次级函数概念理解调用顺序:主级函数>init>次级函数 详细文章传送门:http...

  • Kotlin 之旅5--高阶函数

    高阶函数的基本概念 类似于数学中的高阶函数f(g(x)),高阶函数的概念是: 在Kotlin中,函数可以自由传递、...

  • Kotlin 进阶之路9 高阶函数

    Kotlin 进阶之路 目录 高阶函数的基本概念 传入或者返回函数的函数 函数引用 ::println 带有Rec...

  • Kotlin inline 内联函数

    引言 Kotlin引入了内联函数,但这并不是Kotlin独有的,早在C++中就有了内联函数这一概念,那究竟什么时内...

  • Kotlin 入门

    【Kotlin 入门】 本文介绍了Kotlin入门应该知道一些基本语法概念。包括变量、常量、函数、空安全、类定义、...

  • 快速学习Kotlin(三)函数

    在Kotlin中没有方法这个概念,转而存在的是函数的概念。 函数的声明 我们声明一个函数,使用fun关键字来修饰函...

  • Kotlin 基础学习总结(一)

    前言 本篇文章将介绍 Kotlin 的基本语法中函数,可见性修饰符以及顶层函数的概念,并与 Java 中的相关概念...

  • 2021-11-30(Kotlin学习笔记)Kotlin 基础(

    kotlin java 隔离 kotlin 扩展函数 java 中调用扩展函数 高级函数

  • Kotlin开发Android(一)

    前面几篇对Kotlin做了基础性的讲解,后续会带来开发Android的分享。 一、扩展函数 扩展函数概念:Kotl...

网友评论

      本文标题:Kotlin概念----函数

      本文链接:https://www.haomeiwen.com/subject/hkaghltx.html