为什么 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/