在Java和Android,我们可以这样做:如何在kotlin中“静态”访问类的成员?
public static MyApplication extends Application {
private static Context appContext;
public void onCreate() {
appContext = this;
}
public static Context getAppContext() {
return appContext;
}
}
,这样,在其他地方,我们可以这样做:
appContext = MyApplication.getAppContext();
我们如何在科特林做到这一点?过去一小时左右,我一直在圈圈。
在此先感谢。
//编辑 也许我应该更清楚。我的意思是我们如何在Kotlin中编写上述代码并在Kotlin中使用它。
就叫appContext。我认为Kotlin与外部类一起工作来完成像kotlin这样的调用(但是可能的话在Java字节代码中使用getter)。调用MyApplication。appContext应该足够了 – Zoe
重复的https://stackoverflow.com/questions/40352684/what-is-the-equivalent-of-java-static-methods-in-kotlin? – Xvolks
@Xvolks问题是关于在Java代码中访问一个恰好是私有的静态字段。这不是重复的。 – Zoe