Reflection 如何获得任何课程?Kotlin中的变量?

Reflection 如何获得任何课程?Kotlin中的变量?,reflection,kotlin,kotlin-null-safety,Reflection,Kotlin,Kotlin Null Safety,我想让我的equals也比较类,我写了 override fun equals(other: Any?): Boolean { return this::class == other::class && ... } 不幸的是,它发誓 Expression in a class literal has a nullable type 'Any?', use !! to make the type non-nullable 但是我也要与nulls

我想让我的
equals
也比较类,我写了

    override fun equals(other: Any?): Boolean {
        return this::class == other::class && ...

    }
不幸的是,它发誓

Expression in a class literal has a nullable type 'Any?', use !! to make the type non-nullable

但是我也要与
null
s进行比较。那光荣的“零安全”呢?他们忘记了是为了反思?我没有找到
?:
操作符或其他什么东西。

想想这个。类实际上在
字符串
字符串
之间没有区别,只是类型不同。您不能在可空类型上调用该运算符,因为这可能意味着您在
null
上调用它,这将导致
NullPointerException

val x: String? = null
x!!::class //throws NPE
借助范围函数
let
,您可以确保它不是
null
,并使用:


?:
通过使表达式
false
(不相等)来处理
null
的情况。

这个equals方法在不同的类中重用吗?@D3xter在子类中是
return other?.let { this::class == other::class } ?: false