gpt4 book ai didi

android - 如何在Kotlin中定义应用程序类和静态变量

转载 作者:行者123 更新时间:2023-12-02 13:22:16 26 4
gpt4 key购买 nike

如何在kotlin中编写等效代码,我需要使用定义的静态变量

public class ThisForThatApplication extends Application {

static ThisForThatApplication appInstance;

public static ThisForThatApplication getAppInstance() {
if (appInstance == null) {
appInstance = new ThisForThatApplication();
}
return appInstance;
}
}

最佳答案

试试这个

class ThisForThatApplication : Application() {

companion object {

@JvmField
var appInstance: ThisForThatApplication? = null



@JvmStatic fun getAppInstance(): ThisForThatApplication {
return appInstance as ThisForThatApplication
}
}

override fun onCreate() {
super.onCreate()
appInstance=this;
}

}

有关更多信息,请阅读 Static Fields Static Methods

关于android - 如何在Kotlin中定义应用程序类和静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53827853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com