class B
val a: A = A()
if (a is A) { /* do something */ }
when (a) {
someValue -> { /* do something */ }
is B -> { /* do something */ }
else -> { /* do something */ }
}
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
}
然后在main函数中,我尝试打印并在终端上显示它:
fun main() {
val stringResult = getResult("Kotlin")
val intResult = getResult(100)
// TODO 2
println(stringResult)
println(intResult)
}