为什么我们编写NextActivity :: class.java,尽管这是一个kotlin类? [英] Why we write NextActivity::class.java although this is a kotlin class?
问题描述
我们必须以.java扩展名写这行,尽管其扩展名是.kt.我认为Kotlin文件可以转换为java文件,但是java也可以转换为字节码,因此我们也可以使用.class文件如果Kotlin代码可以转换进入Java代码.
We have to write this line with the extension .java although its extension is .kt I think Kotlin file converts into the java file but the java also converts in byte code so we can also use .class file If the Kotlin code converts into the java code.
NextActivity::class.java
至 NextActivity::class.kt //not worked
btn?.setOnClickListener {
startActivity(Intent(this, NextActivity::class.java))
}
所以问题是为什么我们在NextActivity::class.java
So the question is why we write the .java in NextActivity::class.java
问题来自此处.
推荐答案
因为您要访问我认为Kotlin并没有从头开始重新实现它们,因此,要访问它们,您必须将kotlin类反射"为Java类.
I think they are not reimplemented from scratch in Kotlin so, in order to access them, you have to "reflect" your kotlin class to a Java one.
NextActivity::class
返回KClass
引用,并且KClass
具有
NextActivity::class
returns the KClass
reference and the KClass
has the property java
and the Intent
contructor signature is Intent(Context packageContext, Class cls)
so the second parameter is Class
type So the final answer would be this is not the extension this is just property.
这篇关于为什么我们编写NextActivity :: class.java,尽管这是一个kotlin类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!