Kotlin:相当于KClass的getClass()。
In Java we can resolve a variable's class through getClass()
like something.getClass()
. In Kotlin I am aware of something.javaClass
which is nice but I want to be able to get the KClass
in a similar way. I've seen the Something::class
syntax but this is not what I need. I need to get the KClass of a variable. Does such functionality exist?
在Java中,我们可以通过getClass()来解析变量的类。在科特林,我知道一些事情。javaClass很好,但是我希望能够以类似的方式获得KClass。我已经看到了一些东西:类语法,但这不是我需要的。我需要得到一个变量的KClass。这样的功能存在吗?
2 个解决方案
#1
57
The easiest way to achieve this since Kotlin 1.1 is the class reference syntax:
实现这一点的最简单方法是,Kotlin 1.1是类引用语法:
something::class
If you use Kotlin 1.0, you can convert the obtained Java class to a KClass instance by calling the .kotlin
extension property:
如果使用Kotlin 1.0,可以通过调用.kotlin扩展属性将获得的Java类转换为KClass实例:
something.javaClass.kotlin
更多相关文章
- java中的成员变量和局部变量的区别
- 如何在另一个类中使用静态类中的变量?
- JAVA 静态方法和静态变量和final和※静态import※
- Linux(Centos7.X ) 配置Java 环境变量
- java通过映射取得方法对一个类的变量进行赋值
- JavaScript基础——变量、作用域和内存问题
- 【读书笔记】JavaScript权威指南 第6版 (三)类型、值和变量
- 关于Javascript中声明变量、函数的笔记
- Servlet也是一个Java类.为什么我不可以将Serlvet中的变量传递出