In kotlin class, I have method parameter as object (See kotlin doc here ) for class type T. As object I am passing different classes when I am calling method. In Java we can able to compare class using instanceof of object which class it is.

So I want to check and compare at runtime which Class it is?

我怎么才能查到Kotlin 的instanceof节课呢?

推荐答案

使用is.

if (myInstance is String) { ... }

or the reverse !is

if (myInstance !is String) { ... }

Kotlin相关问答推荐

外键是主键的一部分,但不是索引的一部分.房间

在调用父构造函数之前重写类属性

Mockk:对同一函数进行两次存根会忽略第一个行为

为何Kotlin标准库中的AND和OR函数不能像&&和||一样进行短路运算?

在kotlin中匹配多个变量

Eclipse:无法安装 Kotlin 插件

如何从 Java 中隐藏 Kotlin 的 lateinit var 支持字段?

Kotlinwhen表达式在使用主题时是否支持复合布尔表达式?

runOnUiThread 没有调用

在用Kotlin编写的Android库公共API中处理R8+JvmStatic Annotation+Lambda

Kotlin内联扩展属性

从 Spring WebFlux 返回 Flux 返回一个字符串而不是 JSON 中的字符串数组

使用Dagger 2提供函数依赖性

如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动?

如何获取Kotlin中变量的名称?

如何在特定条件下清除jetpack数据存储数据

使用导航组件在BottomNavigationView中传递参数

为什么 Kotlin 会收到这样的 UndeclaredThrowableException 而不是 ParseException?

Kotlin:获取文件的扩展名,例如.txt

Kotlin for assertThat(foo, instanceOf(Bar.class))