为什么我们编写 NextActivity::class.java 虽然这是一个 kotlin 类? [英] Why we write NextActivity::class.java although this is a kotlin class?

查看:43
本文介绍了为什么我们编写 NextActivity::class.java 虽然这是一个 kotlin 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须用扩展名 .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 to NextActivity::class.kt//不工作

btn?.setOnClickListener {
   startActivity(Intent(this, NextActivity::class.java))
}

所以问题是我们为什么要在 NextActivity::class.java

So the question is why we write the .java in NextActivity::class.java

问题来自此处.

推荐答案

因为要访问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 具有 property javaIntent构造函数签名是 Intent(Context packageContext, Class cls) 所以第二个参数是 Class 类型 所以最终的答案是 这不是扩展,这只是属性.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆