Standard的apply函数

Posted zhangjin1120

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Standard的apply函数相关的知识,希望对你有一定的参考价值。

apply是函数吗?

是的,内联扩展函数

怎么用?

下面是kotlin和java的转化:

//kotlin

fun main(args: Array<String>) 
    val user = User("Kotlin", 1, "1111111")

    val result = user.apply 
        println("my name is $name, I am $age years old, my phone number is $phoneNum")
        1000
    
    println("result: $result")


//java

public final class ApplyFunctionKt 
   public static final void main(@NotNull String[] args) 
      Intrinsics.checkParameterIsNotNull(args, "args");
      User user = new User("Kotlin", 1, "1111111");
      String var5 = "my name is " + user.getName() + ", I am " + user.getAge() + " years old, my phone number is " + user.getPhoneNum();
      System.out.println(var5);
      String var3 = "result: " + user;
      System.out.println(var3);
   


Kotlin系列之let、with、run、apply、also函数的使用

以上是关于Standard的apply函数的主要内容,如果未能解决你的问题,请参考以下文章

Kotlin标准库函数 ① ( apply 标准库函数 | let 标准库函数 )

Kotlin标准库函数总结 ( apply 函数 | let 函数 | run 函数 | with 函数 | also 函数 | takeIf 函数 | takeUnless 函数 )

Kotlin标准库函数总结 ( apply 函数 | let 函数 | run 函数 | with 函数 | also 函数 | takeIf 函数 | takeUnless 函数 )

Kotlin的标准函数和静态方法

Kotlin标准函数

Kotlin 初学者标准函数