我首先有一个java类。
First.java
`class First{
public static TAG = "test"
}`Second.kt
`class Second{
First.TAG /*Error classifier First does not have a companion object,and thus must be initialized here*/
}`因此,请帮助我在Second.kt kotlin类中调用First.java中声明的静态变量标记
发布于 2018-07-18 18:21:08
Java类:
class First {
public static String TAG = "test";
}Kotlin类:
class Second {
val secondTag: String = First.TAG
}而且没有任何问题。
尝试使用IntelliJ IDEA
fun main(args: Array < String > ) {
val s = Second()
println(s.secondTag)
}打印test
发布于 2018-07-20 07:24:08
只需创建一个适当的、静态的最后一个常量。
class First {
public static final String TAG = "test"
}现在您可以从Kotlin调用它。
class Second {
fun blah() {
val tag = First.TAG
}
}发布于 2019-10-16 14:50:58
头等舱
package com.your.package object First { val TAG: String= "Your TAG"; }
第二类
class Second{ First.TAG }
Kotlin没有静态成员或成员函数,您可以使用伴生对象
https://stackoverflow.com/questions/51398326
复制相似问题