相关疑难解决方法(0)

函数中单元返回的目的是什么

来自Kotlin文档:

如果函数没有返回任何有用的值,则其返回类型为Unit.Unit是只有一个值的类型 - Unit.VALUE.不必显式返回此值:

fun printHello(name : String?) : Unit { 
   if (name != null) 
     print("Hello, $name!") 
   else 
     print("Hi there!") 
   // We don't need to write 'return Unit.VALUE' or 'return', although we could 
}
Run Code Online (Sandbox Code Playgroud)

单位返回功能的目的是什么?为什么有VALUE?什么是VALUE?

kotlin

53
推荐指数
3
解决办法
1万
查看次数

标签 统计

kotlin ×1