【发布时间】:2019-10-18 04:29:46
【问题描述】:
我在 Kotlin 中创建了一个应用程序类。我需要访问从应用程序中的任何位置返回变量的方法。
问题是我无法从程序的其他部分访问该方法。
当代码用 Java 编写时,我可以访问,但是当代码用 Kotlin 编写时,Application 类中的方法无法访问。
请参考以下代码:
类 MyRetroApplication : Application() {
lateinit var apiComponent:APIComponent
companion object {
var ctx: Context? = null
}
override fun onCreate() {
super.onCreate()
ctx = applicationContext
apiComponent = initDaggerComponent()
}
fun getMyComponent(): APIComponent {
return apiComponent
}
fun initDaggerComponent():APIComponent{
apiComponent = DaggerAPIComponent
.builder()
.aPIModule(APIModule(APIURL.BASE_URL))
.build()
return apiComponent
} }
在上面的代码中,如何在 Kotlin 中全局访问函数 getMyComponent()。
【问题讨论】: