Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Kotlin中变量的类型_Kotlin_Instanceof - Fatal编程技术网

获取Kotlin中变量的类型

获取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

如何在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}")    // "Int"
println("${number::class.qualifiedName}") // "kotlin.Int"
请注意:

在Java平台上,使用 反射特性作为一个单独的JAR文件分发 (kotlin reflect.jar)。这样做是为了减小所需的 不使用反射功能的应用程序的运行时库。 如果确实使用反射,请确保添加了.jar文件 到项目的类路径


来源:

您可以使用
properties::class.simpleName
获取类名,也可以使用properties.javaClass.simpleName