gpt4 book ai didi

kotlin - 在 Kotlin 中, "::class.simpleName"是做什么的?

转载 作者:行者123 更新时间:2023-12-05 01:24:23 25 4
gpt4 key购买 nike

val var1: Any = "Carmelo Anthony"

我的印象是 ::class.simpleName 返回对象的变量类型
当我执行以下操作时:

val var1Type = var1::class.simpleName
打印(var1Type)

我得到 String 而不是 Any

但是当我这样做的时候

val var2: String = var1

我得到一个类型不匹配:推断类型是 Any 但应为 String

最佳答案

  • 在 Kotlin 中,::class运算符以两种形式存在:
  • 在您的情况下,var1运行时类型为 String但是 Any 的静态类型.
    • 所以 var1::class返回 KClass对于 String , 不是 Any .
  • 但是 Kotlin 的类型系统,像大多数静态类型语言一样,不允许隐式收缩转换(即给定一个类型为 var2 的变量 String,您不能从另一个变量(var2)赋值给 var3。 ) 静态类型为 Any ,因为 var3 可能 具有与 String 完全不兼容的运行时类型,例如 InputStream 对象。
    • ...即使可以证明(通过手动执行程序)Any -typed 值将始终是 String .
    • 幸运的是,Kotlin 的类型检查器是现代的 and its "Smart cast" feature follows the scope of type-narrowingis使用了运算符,这很简洁(TypeScript 也有,但我认为其他任何语言都没有)。
      • 在您无法使用智能转换或可以通过其他方式证明自己向下转换是安全的情况下,请使用 as运算符(operator)执行 unsafe cast .像这样:var2: String = var1 as String .
        • (有点令人困惑,其他语言使用 as 作为安全 转换的运算符,argh)。

In context :

fun main() {

val var1: Any = "Carmelo Anthony"
val var1Type = var1::class.simpleName
println("var1's type: " + var1Type) // <-- This will print the *runtime type* of `var1` (String), not its static type (which is `Any`, *not* `String`).

/*
val var2: String = var1 // <-- Fails beause `var1` is `Any`, and `Any` is "wider" than `String`, and narrowing conversions always considered unsafe in languages like Kotlin, Java, etc.
*/
val var2Unsafe: String = var1 as String; // <-- Doing this is unsafe because it will throw if `var1` is not a String.
val var2Safe : String? = var1 as? String; // <-- Doing this is safe because it `var2Safe` will be null if `var1` is not a String.

println(var2Unsafe)
println(var2Safe)
}

如果您熟悉其他语言,那么这里有一个不完整的等效操作及其语法表:

<表类="s-表"><头><日> Kotlin JavaJavaScript<日>C# <日>C++ <正文>获取静态类型 TypeName::class TypeName.class ConstructorName typeof(TypeName) typeid(TypeName) 获取运行时类型 variableName::class variableName.getClass() typeof variableName (内在) variableName.constructor (对象) variableName.GetType() typeid(variableName) 从名称(字符串)获取类型 Class.forName( typeName ).kotlin Class.forName( typeName ) eval( typeName ) (永远不要这样做)静态定义的运行时类型检查 variableName is TypeName variableName instanceof TypeName typeof variableName === 'typeName' (内在函数)或 variableName instanceof ConstructorName (对象) variableName is TypeName 运行时动态类型检查 otherKClass.isInstance( variableName )otherKType.isSubtypeOf() otherClass.isAssignableFrom( variableName.getClass() ) otherType.IsAssignableFrom( variableName.GetType() ) 不安全的缩小(又名沮丧) val n: NarrowType = widerVar as NarrowType; NarrowType n = (NarrowType)widerVar; variableName as TypeName (仅限 typescript ) NarrowType n = (NarrowType)widerVar; 安全收窄(向下或 null ) val n: NarrowType? = widerVar as? NarrowType; NarrowType n? = widerVar as NarrowType; dynamic_cast<NarrowType>( widerVar ) 有条件地缩小范围 variableName is TypeName func(x: unknown): x is TypeName守卫函数(仅限 TypeScript) widerVar is TypeName n

关于kotlin - 在 Kotlin 中, "::class.simpleName"是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71405186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com