获取Kotlin中变量的类型
如何在Kotlin中找到变量类型? Java中有获取Kotlin中变量的类型,kotlin,instanceof,Kotlin,Instanceof,如何在Kotlin中找到变量类型? Java中有instanceof,但Kotlin不存在: val properties = System.getProperties() // Which type? 可以使用检查对象是否为特定类型: val number = 5 if(number is Int) { println("number is of type Int") } 您还可以使用反射获取类型为String: println("${number::class.simpleName
instanceof
,但Kotlin不存在:
val properties = System.getProperties() // Which type?
可以使用检查对象是否为特定类型:
val number = 5
if(number is Int) {
println("number is of type Int")
}
您还可以使用反射获取类型为String
:
println("${number::class.simpleName}") // "Int"
println("${number::class.qualifiedName}") // "kotlin.Int"
请注意:
在Java平台上,使用
反射特性作为一个单独的JAR文件分发
(kotlin reflect.jar)。这样做是为了减小所需的
不使用反射功能的应用程序的运行时库。
如果确实使用反射,请确保添加了.jar文件
到项目的类路径
来源:您可以使用
properties::class.simpleName
获取类名,也可以使用properties.javaClass.simpleName