gpt4 book ai didi

java - Android,无法使静态 getter setter 属性起作用吗?

转载 作者:行者123 更新时间:2023-11-29 19:28:19 28 4
gpt4 key购买 nike

我正在尝试这个...

public class Info {
private static Info ourInstance = new Info();
public static Info getInstance() { return ourInstance; }

private static int currentIndex;

public static void setCurrentIndex(int i) {
Log.d("DEV", "setter!");
currentIndex = i;
// do other work here
}

public static int getCurrentIndex() {
Log.d("DEV", "getter!");
return currentIndex;
}

private Info() {
Log.d("DEV", "class initialized no problem...");
currentIndex = 42; // just doesn't work, only sets the field
}

}

在任何其他类中...

Info.currentIndex = 666; // just doesn't work

它根本不起作用 - 可能是什么问题?什么都试过了。

最佳答案

如果你最终要这样做,为什么还要定义 setter/getter?

Info.currentIndex = 666;

如果是,则将currentIndex 可见性更改为公开...

或者更好的是,与代码保持一致

Info.setCurrentIndex(666);

关于java - Android,无法使静态 getter setter 属性起作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40852452/

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