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

更多相关文章

  1. java中的成员变量和局部变量的区别
  2. 如何在另一个类中使用静态类中的变量?
  3. JAVA 静态方法和静态变量和final和※静态import※
  4. Linux(Centos7.X ) 配置Java 环境变量
  5. java通过映射取得方法对一个类的变量进行赋值
  6. JavaScript基础——变量、作用域和内存问题
  7. 【读书笔记】JavaScript权威指南 第6版 (三)类型、值和变量
  8. 关于Javascript中声明变量、函数的笔记
  9. Servlet也是一个Java类.为什么我不可以将Serlvet中的变量传递出

随机推荐

  1. 国庆,我决定“颓废”一段时间!
  2. 实用小技巧,Python一秒将全部中文姓名转为
  3. 原理+代码|详解层次聚类及Python实现
  4. 实战|手把手教你用Python爬取存储数据,还能
  5. 自学系列|兴趣能不能转化为工作?
  6. 使用这个 Python 库,只需一行命令,给头像戴
  7. 天秀!一张图就能彻底搞定Pandas!
  8. PySimpleGUI | 用Python玩转GUI开发,为什么
  9. 一篇长文带你在python里玩转Json数据
  10. PySimpleGUI 进阶| 原来用Python做一个图