英文:
what does :: mean ? why do i have .java at the end?
问题
我正在尝试发送一个简单的显式意图以启动一个新的活动,我之前习惯用Java编码,语法非常简单,我最近切换到Kotlin,对于末尾的"::"或".java"扩展名究竟是做什么的一无所知?
fun Run(view: View) {
// 这里的 :: 是什么意思?为什么末尾有 .java 扩展名?
val intent: Intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
英文:
I am trying to send a simple explicit intent to start a new activity , I previously used to code in Java & the syntax was pretty simple , I switched to kotlin recently & I have no idea what the :: or the .java extension at the end is doing ?
fun Run(view: View) {
// what the heck is :: ? why do i have a .java at the end ?
val intent:Intent= Intent(this, MainActivity2::class.java)
startActivity(intent)
}
答案1
得分: 2
这里的作用是获取对 MainActivity2
的引用。在 Java 中,你会使用 MainActivity2.class
来实现这一点。
在 Kotlin 中,MainActivity::class
返回一个 KClass 类型的值,这与 Java 的类引用是不同的。但是 Intent
函数需要一个 Java 类引用,所以你可以在引用上使用 .java
属性来获得 Java 类引用。
英文:
::
creates a member reference or a class reference
Here what it does is getting the reference to MainActivity2
. In Java you'd do this with MainActivity2.class
.
In Kotlin, MainActivity::class
returns a value of type KClass, which is different from Java class reference. But the Intent
function expects a Java class reference, so you use the .java
property on the reference to get the Java class reference
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论