Android,无法使静态 getter setter 属性 工作?
Android, couldn't make static getter setter property work?
我正在尝试这个...
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
}
}
在任何其他 class...
Info.currentIndex = 666; // just doesn't work
它就是不起作用 - 可能是什么问题?什么都试过了。
如果你最终要这样做,为什么要定义 setter/getter?
Info.currentIndex = 666;
如果是,则将 currentIndex 可见性更改为 public...
甚至更好,与代码一致并执行
Info.setCurrentIndex(666);
我正在尝试这个...
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
}
}
在任何其他 class...
Info.currentIndex = 666; // just doesn't work
它就是不起作用 - 可能是什么问题?什么都试过了。
如果你最终要这样做,为什么要定义 setter/getter?
Info.currentIndex = 666;
如果是,则将 currentIndex 可见性更改为 public...
甚至更好,与代码一致并执行
Info.setCurrentIndex(666);