在Kotlin中,可以使用is
判断对象是否是类(包括继承)的实例
myObject is String
But how can you check, if two objects are of the exact same class? I am searching for an analogue to Python's
type(obj1) is type(obj2)
在Kotlin中,可以使用is
判断对象是否是类(包括继承)的实例
myObject is String
But how can you check, if two objects are of the exact same class? I am searching for an analogue to Python's
type(obj1) is type(obj2)
您可以获得::class
的对象类型,并比较:
val sameClass = obj1::class == obj2::class
More specifically, this section of the above documentation describes that ::class
on an object gives you exactly what you want, the exact class of the instance you're calling it on.