“::"是什么?在 kotlin 中是什么意思? [英] what does "::" mean in kotlin?

查看:42
本文介绍了“::"是什么?在 kotlin 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Kotlin 的新手
我使用此代码打开另一个活动:

startActivity(Intent(this,IntroAndLang::class.java))

当前活动和目标活动是用 Kotlin 编写的

我不明白为什么在 IntroAndLang::class.java

中没有单个 : 而不是 ::

解决方案

docs 这是一个类参考:

<块引用>

类参考:最基本的反射功能是获取 Kotlin 类的运行时引用.要获取对静态已知 Kotlin 类的引用,您可以使用类文字语法:

val c = MyClass::class//引用是一个KClass类型的值.

<块引用>

请注意,Kotlin 类引用与 Java 类引用不同.要获取 Java 类引用,请使用 KClass 实例上的 .java 属性.

也是方法引用的语法,如这个简单的例子:

list.forEach(::println)

指的是在 Kotlin 标准库中定义的 println.

I'm new to Kotlin
I used this code for opening another activity:

startActivity(Intent(this,IntroAndLang::class.java))

current activity and target activity are written in Kotlin

I cant understand why there is not single : instead of :: at IntroAndLang::class.java

解决方案

As stated in the docs this is a class reference:

Class References: The most basic reflection feature is getting the runtime reference to a Kotlin class. To obtain the reference to a statically known Kotlin class, you can use the class literal syntax:

val c = MyClass::class
//The reference is a value of type KClass.

Note that a Kotlin class reference is not the same as a Java class reference. To obtain a Java class reference, use the .java property on a KClass instance.

It’s also the syntax for method references as in this simple example:

list.forEach(::println)

It refers to println defined in Kotlin Standard library.

这篇关于“::"是什么?在 kotlin 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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