How to check “instanceof ” class in kotlin?

后端 未结 8 1541
有刺的猬
有刺的猬 2020-12-14 05:14

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 metho

相关标签:
8条回答
  • 2020-12-14 05:57

    Use is.

    if (myInstance is String) { ... }
    

    or the reverse !is

    if (myInstance !is String) { ... }
    
    0 讨论(0)
  • 2020-12-14 05:59

    You can use is:

    class B
    val a: A = A()
    if (a is A) { /* do something */ }
    when (a) {
      someValue -> { /* do something */ }
      is B -> { /* do something */ }
      else -> { /* do something */ }
    }
    
    0 讨论(0)
提交回复
热议问题