kotlin匿名函数,匿名类特殊用法汇总

package com.example.kotlin

/**

  • 根据输入参数的泛型类型决定返回值的泛型类型

  • 根据输入参函数的返回值类型决定函数返回值的泛型类型
    */
    class Test {
    fun main(args: Array) {
    print(aa(“str”, “str2”, xx))
    //最后一个参数如果是函数时,直接写在括号外面
    print(aa(“str”, “str2”) {
    return@aa 10
    })
    print(aa(“str”, “str2”) {
    return@aa “xx”
    })
    }

    private fun aa(str: String, str2: String, bb: () -> T): T {
    return bb()
    }

    private val xx = fun(): Int {
    return 10
    }

    fun main2(args: Array) {
    println(aa(“str”, “str2”, xx))
    println(aa(“str”, “str2”) {
    return@aa 10
    })
    println(aa(“str”, “str2”) {
    return@aa 20
    })
    val string = aa2(“hello”, “world”) {
    “aaaaaaaaaaa”
    }
    val number1 = aa2(“hello”,“world”){
    return@aa2 10
    }
    val number2 = aa2(“hello”, “world”) {
    10
    }
    }

    private fun aa2(str: String, str2: String, bb: () -> T): T {
    println(str)
    println(str2)
    return bb()
    }
    private val xxx = fun(): Int {
    return 10
    }

    //kotlin中匿名类的使用***//

    fun main3(args: Array) {
    test(object : Test {
    override fun test() {
    print(“hello test!”)
    }
    })
    display(object : Display {
    override fun show() {
    print(“hello show”)
    }
    override fun hide() {
    print(“hello hide”)
    }
    })
    var aTest = Test()
    aTest.display(object : Display {
    override fun show() {
    print(“hello show”)
    }
    override fun hide() {
    print(“hello hide”)
    }
    })
    aTest.test(object : Test {
    override fun test() {
    }
    })
    var bTest = TestJava()
    //调用java的代码,匿名类可以用简便的方法
    bTest.test {
    print(“hello java test”)
    }
    //调用java的代码
    bTest.test2(object : TestJava.Test {
    override fun test() {
    print(“hello java test”)
    }
    }, “hello message”)
    /**
    * 在kotlin中调java的代码时,只有一个方法的匿名类作参数和只有一个方法的匿名函数作参数使用方式一样
    *
    * test2((() → Unit)!, String!) defined in com.example.kotlin.TestJava
    * test2(TestJava.Test!, String!) defined in com.example.kotlin.TestJava
    /
    //调用java的类可以这样写,但kotlin匿名类只能是上面一种写法
    bTest.test2({
    print(“hello java test”)
    }, “hello message”)
    //调用java的代码,匿名类作参数,有多个方法时,不能用简易写法。也要用object的方式
    bTest.display(object : TestJava.Display {
    override fun hide() {
    }
    override fun show() {
    }
    })
    xx1(5)
    }
    open fun test(test: Test) {
    test.test()
    }
    open fun display(display: Display) {
    display.show()
    display.hide()
    }
    interface Test {
    fun test()
    }
    interface Display {
    fun show()
    fun hide()
    }
    ///********表达式作为函数体,返回值的类型可以省略,可以利用Kotlin的类型推导功能,推测出函数返回值的类型。
    ***************//
    //最后一句作为返回值
    fun sum(a: Int, b: Int, c: Int) = run {
    print(“hello”)
    a + b + c
    }
    fun sum1(a: Int, b: Int, c: Int): Int {
    return a + b + c
    }
    fun sum2(a: Int, b: Int, c: Int) = a + b + c
    //print没有返回值,所以sum3的返回类型是Unit(空)
    fun sum3(a: Int, b: Int, c: Int) = run {
    val d = a + b + c
    print(“hello”)
    }
    //函数作为返回值
    fun sum4(a: Int, b: Int, c: Int): () -> Unit = {
    val d = a + b + c
    }
    //空返回值
    fun sum5(a: Int, b: Int, c: Int): Unit {
    print(“hello”)
    }
    //匿名函数方式一
    val sumLambda2: (a: Int, b: Int, c: Int) -> Int = { a, b, c ->
    a * b * c
    }
    //匿名函数,方式二
    val sumLambda: (Int, Int, Int) -> Int = { a, b, c -> a + b + c }
    //匿名函数,方式三
    val sumLambda3 = fun(a: Int, b: Int, c: Int): Int {
    return a * b * c
    }
    //匿名函方式一,xx2是一个返回Int的函数,最后一句作为返回值
    val xx1: (a: Int) -> Int = { a ->
    10 * a
    }
    //匿名函方式二,xx2是一个返回Int的函数,最后一句作为返回值
    val xx2: (Int) -> Int = { a ->
    10 * a
    }
    //匿名函数方式三
    val xx3 = fun(a: Int): Int {
    return 10 * a
    }

    //xx4是一个Int变量,函数最后一句是返回值 ,赋值给常量xx3
    //加run就返回函数最后一句的值
    //不加run就返回函数本身,函数返回值的类型就是最后一句的类型
    val xx4: Int = run {
    10
    }

    //方式一,函数自己定返回值,需要显式return
    val yy1 = fun(): Int {
    return 10
    }

    //方式二,被赋值的变量定返回值 ,不需要显式return
    val yy2: () -> Int = {
    10
    }

    //yy3是一个返回值为Int的函数,是方式二的简写,最后一句是函数返回值类型
    val yy3 = {
    10
    }

    //yy4是一个返回值为空的函数,最后一句是返回值
    val yy4 = {
    print(“yy4”)
    }

    //yy5是一个返回值为string的函数,最后一句是返回值
    val yy5 = {
    val ss = “sssssssss”
    ss
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值