从Kotlin访问Integer.class
我有一个Java方法,它接受从Kotlin访问Integer.class,kotlin,Kotlin,我有一个Java方法,它接受Class参数。我需要将Integer.class传递给它,但是来自Kotlin代码。我尝试了Int::class.java,但是这不起作用,因为Int.class被传递给函数。我的问题是,如何从Kotlin访问Integer.class Java void foo(Class clazz); Kotlin foo(Int::class.java) // does not work, int.class gets passed to foo 必须使用获取Kotli
Class
参数。我需要将Integer.class
传递给它,但是来自Kotlin代码。我尝试了Int::class.java
,但是这不起作用,因为Int.class
被传递给函数。我的问题是,如何从Kotlin访问Integer.class
Java
void foo(Class clazz);
Kotlin
foo(Int::class.java) // does not work, int.class gets passed to foo
必须使用获取Kotlin中的包装器类型类:
返回与给定的KClass
实例对应的Java类
实例。如果是原语类型,则返回相应的包装器类
例如:
// v--- java.lang.Integer
println(Int::class.javaObjectType)
// v--- int
println(Int::class.java)
有关更多详细信息,请参见。