- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
val var1: Any = "Carmelo Anthony"
我的印象是 ::class.simpleName
返回对象的变量类型
当我执行以下操作时:
val var1Type = var1::class.simpleName
打印(var1Type)
我得到 String
而不是 Any
但是当我这样做的时候
val var2: String = var1
我得到一个类型不匹配:推断类型是 Any 但应为 String
最佳答案
::class
运算符以两种形式存在:
TypeName::class
- which returns a KClass
object for the static type TypeName
.variableName::class
- which returns a KClass
object corresponding to the runtime type的 variableName
, 而不是 variableName
的静态类型。 (Kotlin 在其文档中将此称为“绑定(bind)类型”)。var1
运行时类型为 String
但是 Any
的静态类型.
var1::class
返回 KClass
对于 String
, 不是 Any
.var2
的变量 String
,您不能从另一个变量(var2
)赋值给 var3
。 ) 静态类型为 Any
,因为 var3
可能 具有与 String
完全不兼容的运行时类型,例如 InputStream
对象。
Any
-typed 值将始终是 String
.is
使用了运算符,这很简洁(TypeScript 也有,但我认为其他任何语言都没有)。
as
运算符(operator)执行 unsafe cast .像这样:var2: String = var1 as String
.
as
作为安全 转换的运算符,argh)。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)
}
如果您熟悉其他语言,那么这里有一个不完整的等效操作及其语法表:
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/
我希望能够从类的伴生对象访问它的 simpleName。 我想要这个: val o1 = Outer("foo") val o2 = Outer("bar") 打印以下输出: Outer: hello
ast.newSimpleName() 方法出现问题。 我无法创建“SomeJava.class”类型的 SimpleName。但该方法对于“SWT.None”或“SomeJava.None”等名称效
val var1: Any = "Carmelo Anthony" 我的印象是 ::class.simpleName 返回对象的变量类型 当我执行以下操作时: val var1Type = var1:
正常的 val 是可以的,并且已初始化。 class MyClass { companion object { private val TAG = MyClass::class
KClass.simpleName 的规范在 Kotlin 1.3.41 for Common、JS、JVM 和 Native 上是: The simple name of the class as
在 java 中,我可以使用 getClass() 然后从该类对象中检索 simpleName 而不会出现任何问题。 String tag = someObject.getClass().getSim
我正在开发一个使用 Thymeleaf 作为模板的 Spring Boot 项目。 在我的实体中,我有 2 个扩展抽象类的子类。 在我的 Html 文件中,当我尝试显示子类的名称时。 Thymelea
我是一名优秀的程序员,十分优秀!