kotlin - 为什么 Kotlin 的代码可以直接从 “kotlin.collections” 调用顶层函数,不需要导入包

标签 kotlin

为什么 Kotlin 的代码可以直接从“kotlin.collections”中调用顶层函数,而无需导入包。例如下面的函数listOf :

data class Person1(val name: String, val age: Int)

class DataClassExecutor {
  ... ...
  fun test(arg: String?): String? {
    val persons = listOf(
        Person1("Lucy", age = 26), 
        Person1("Lily", age = 29))
    ... ...
  }
}

最佳答案

请引用此页面:https://kotlinlang.org/spec/packages-and-imports.html .它说:

There are some packages which have all their entities implicitly imported into any Kotlin file, meaning one can access such entity without explicitly using import directives.


该列表包括 kotlin.collections .
这类似于 Java 中的方式,java.lang是隐式导入的。在Java中不用说java.lang.System.out.println , 只是 System.out.println足够。

关于kotlin - 为什么 Kotlin 的代码可以直接从 “kotlin.collections” 调用顶层函数,不需要导入包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64382283/

相关文章:

android - Android Kotlin将数据从Viewpager的基本 fragment 传递到其Tab fragment 之一

android - MainCoroutineRule 和 runBlocking 的区别

kotlin - BackoffSupervisor可以有多个子 Actor 吗?

field - Kotlin - 如何使外部类的字段只读

kotlin - 启用 proguard 后无法访问 kotlin 类中使用的伴随对象

android - 如何在Android中使用MPAndroidChart使用两组数据绘制图形

kotlin - Kotlin 的 MutableList add() 函数是否总是添加到列表的末尾?

Android Studio 不断显示缺少权限,即使我正在明确处理它。我如何解决它?

javascript - 为 JS 和 Java 编写代码

kotlinx.serialization ,编码解码 SnapshotStateList