如何在 kotlin 中检查 "instanceof " class?
How to check "instanceof " class in kotlin?
在 kotlin class 中,我将方法参数作为对象(参见 kotlin 文档 here)用于 class 类型 T。作为对象,我在调用方法时传递了不同的 classes。
在 Java 中,我们可以使用 class 对象的 instanceof
来比较 class。
所以我想在运行时检查并比较 Class 是哪个?
如何在 kotlin 中检查 instanceof
class?
使用is
。
if (myInstance is String) { ... }
或反过来!is
if (myInstance !is String) { ... }
尝试使用名为 is
的关键字
Official page reference
if (obj is String) {
// obj is a String
}
if (obj !is String) {
// // obj is not a String
}
我们可以使用 is
运算符或其否定形式 !is
.
在运行时检查对象是否符合给定类型
示例:
if (obj is String) {
print(obj.length)
}
if (obj !is String) {
print("Not a String")
}
自定义对象的另一个示例:
让我有一个 obj
类型 CustomObject
。
if (obj is CustomObject) {
print("obj is of type CustomObject")
}
if (obj !is CustomObject) {
print("obj is not of type CustomObject")
}
您可以使用 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 */ }
}
合并 when
和 is
:
when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}
你可以这样查看
private var mActivity : Activity? = null
然后
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is MainActivity){
mActivity = context
}
}
其他解决方案:KOTLIN
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
if (fragment?.tag == "MyFragment")
{}
您可以在此处阅读 Kotlin 文档 https://kotlinlang.org/docs/reference/typecasts.html。我们可以使用 is
运算符或其否定形式 !is
在运行时检查对象是否符合给定类型,例如使用 is
:
fun <T> getResult(args: T): Int {
if (args is String){ //check if argumen is String
return args.toString().length
}else if (args is Int){ //check if argumen is int
return args.hashCode().times(5)
}
return 0
}
然后在主要功能中,我尝试打印并在终端上显示它:
fun main() {
val stringResult = getResult("Kotlin")
val intResult = getResult(100)
// TODO 2
println(stringResult)
println(intResult)
}
这是输出
6
500
您可以将任何 class 与以下函数进行比较。
fun<T> Any.instanceOf(compared: Class<T>): Boolean {
return this::class.java == compared
}
// When you use
if("test".isInstanceOf(String.class)) {
// do something
}
在 kotlin class 中,我将方法参数作为对象(参见 kotlin 文档 here)用于 class 类型 T。作为对象,我在调用方法时传递了不同的 classes。
在 Java 中,我们可以使用 class 对象的 instanceof
来比较 class。
所以我想在运行时检查并比较 Class 是哪个?
如何在 kotlin 中检查 instanceof
class?
使用is
。
if (myInstance is String) { ... }
或反过来!is
if (myInstance !is String) { ... }
尝试使用名为 is
的关键字
Official page reference
if (obj is String) {
// obj is a String
}
if (obj !is String) {
// // obj is not a String
}
我们可以使用 is
运算符或其否定形式 !is
.
示例:
if (obj is String) {
print(obj.length)
}
if (obj !is String) {
print("Not a String")
}
自定义对象的另一个示例:
让我有一个 obj
类型 CustomObject
。
if (obj is CustomObject) {
print("obj is of type CustomObject")
}
if (obj !is CustomObject) {
print("obj is not of type CustomObject")
}
您可以使用 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 */ }
}
合并 when
和 is
:
when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}
你可以这样查看
private var mActivity : Activity? = null
然后
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is MainActivity){
mActivity = context
}
}
其他解决方案:KOTLIN
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
if (fragment?.tag == "MyFragment")
{}
您可以在此处阅读 Kotlin 文档 https://kotlinlang.org/docs/reference/typecasts.html。我们可以使用 is
运算符或其否定形式 !is
在运行时检查对象是否符合给定类型,例如使用 is
:
fun <T> getResult(args: T): Int {
if (args is String){ //check if argumen is String
return args.toString().length
}else if (args is Int){ //check if argumen is int
return args.hashCode().times(5)
}
return 0
}
然后在主要功能中,我尝试打印并在终端上显示它:
fun main() {
val stringResult = getResult("Kotlin")
val intResult = getResult(100)
// TODO 2
println(stringResult)
println(intResult)
}
这是输出
6
500
您可以将任何 class 与以下函数进行比较。
fun<T> Any.instanceOf(compared: Class<T>): Boolean {
return this::class.java == compared
}
// When you use
if("test".isInstanceOf(String.class)) {
// do something
}