为什么Intent中的类带有“::class.java”后缀?

Zor*_*gan 2 android kotlin

Kotlin 中的意图:

val intent = Intent(this, OtherActivity::class.java)
Run Code Online (Sandbox Code Playgroud)

为什么不能是:

val intent = Intent(this, OtherActivity)
Run Code Online (Sandbox Code Playgroud)

Leo*_*Aso 5

该 Intent 构造函数的第二个参数需要您要创建的 Activity 的类。虽然仅使用类名来获取类会很方便OtherActivity,但 Java(和 Kotlin)语法不支持这一点。

相反,Java 为Kotlin 类提供了.class( OtherActivity.class) 语法,Kotlin 为 Java 类提供了( ) 语法,这正是 Intent 构造函数所需要的。::class::class.javaOtherActivity::class.java