• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

kotlin函数作为参数和函数作为返回值练习

武飞扬头像
汤米粥
帮助1

kotlin中函数作为参数和函数作为返回值,在工作中写代码有时会被卡住,怎么写都提示语法错误,今天专门研究一下几种常用的用法。

  1.  
    package com.example.test
  2.  
     
  3.  
    import android.util.Log
  4.  
    import java.util.*
  5.  
     
  6.  
    class KotlinSample {
  7.  
     
  8.  
    //直接定义函数
  9.  
     
  10.  
    //带参数,没有返回值的函数
  11.  
    private var myPrint: (msg: String) -> Unit = { msg -> Log.e("xxx5",msg) }
  12.  
     
  13.  
    //带参数,参数为msg,有返回值的函数类型为Strnig
  14.  
    private var hello : (msg: String) -> String = { "hello ${it.uppercase()}!!" }
  15.  
     
  16.  
    //getIntance返回一个函数,这个函数参数为空,返回值类型为ShoppingMallFloatView
  17.  
    fun getInstance(context: Context): () -> ShoppingMallFloatView = {
  18.  
    instance ?: synchronized(this) {
  19.  
    instance ?: ShoppingMallFloatView(context).also {
  20.  
    instance = it
  21.  
    }
  22.  
    }
  23.  
     
  24.  
    }
  25.  
     
  26.  
     
  27.  
    fun callMethods() {
  28.  
     
  29.  
    method1 {
  30.  
    var hello = "hello world"
  31.  
    Log.e("xxx1", hello)
  32.  
    hello
  33.  
    }
  34.  
     
  35.  
    method2 {
  36.  
    var hello = "hello world"
  37.  
    Log.e("xxx2", hello)
  38.  
    }
  39.  
     
  40.  
    method3("hello world") { msg ->
  41.  
    Log.e("xxx3", msg)
  42.  
    }
  43.  
     
  44.  
    //函数作为返回值
  45.  
    var method4 = method4("hello world")
  46.  
    method4.invoke()
  47.  
     
  48.  
    //使用定义的函数
  49.  
    myPrint.invoke("hello world")
  50.  
     
  51.  
    var greet = hello("zhang san")
  52.  
    myPrint.invoke(greet)
  53.  
     
  54.  
    //参数为一个数字 加 一个函数
  55.  
    test(2) { a: Int, b: Int ->
  56.  
    var num = (a b)*5
  57.  
    num
  58.  
    }
  59.  
    }
  60.  
     
  61.  
    //函数作为参数,返回String 但是不需要return 直接将要返回的值放在最后一行
  62.  
    private fun method1(method: () -> String) {
  63.  
    method.invoke()
  64.  
    }
  65.  
     
  66.  
    //函数作为参数,Unit表示没有返回值
  67.  
    private fun method2(method: () -> Unit) {
  68.  
    method.invoke()
  69.  
    }
  70.  
     
  71.  
    //函数作为参数,有一个输入参数时。不能直接带给它,需要另外一个参数传进来。
  72.  
    private fun <T> method3(msg1: T, method: (msg: T) -> Unit) {
  73.  
    method.invoke(msg1)
  74.  
    }
  75.  
     
  76.  
     
  77.  
    //函数作为返回值
  78.  
    private fun method4(str: String): () -> Unit {
  79.  
    Log.e("xxx4", "这一部分不返回,直接运行")
  80.  
    return {
  81.  
    var strNew = str.uppercase(Locale.getDefault())
  82.  
    Log.e("xxx4", strNew)
  83.  
    }
  84.  
    }
  85.  
     
  86.  
    fun test(a : Int , b : (num1 : Int , num2 : Int) -> Int) : Int{
  87.  
    return a b.invoke(3,5) //8
  88.  
    }
  89.  
     
  90.  
    }
学新通

运行结果:

  1.  
    2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx1: hello world
  2.  
    2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx2: hello world
  3.  
    2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx3: hello world
  4.  
    2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx4: 这一部分不返回,直接运行
  5.  
    2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx4: HELLO WORLD
  6.  
    2022-07-28 11:05:04.309 20589-20589/com.example.test E/xxx5: hello world
  7.  
    2022-07-28 11:05:04.309 20589-20589/com.example.test E/xxx5: hello ZHANG SAN!!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbjhhe
系列文章
更多 icon
同类精品
更多 icon
继续加载