Android,无法使静态getter setter属性正常工作吗? [英] Android, couldn't make static getter setter property work?

查看:100
本文介绍了Android,无法使静态getter setter属性正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试...

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
    }

}

在任何其他班上...

in any other class...

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

那根本不起作用-问题可能是什么?尝试了一切.

It just doesn't work - what could the problem be? Tried everything.

推荐答案

如果最终要执行此操作,为什么要定义setter/getter?

Why do you define the setter/getter if you are going to end up doing this?

Info.currentIndex = 666;

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

if so, then change currentIndex visibility to public...

甚至更好,请与代码保持一致并做到

or even better, be congruent with the code and do

Info.setCurrentIndex(666);

这篇关于Android,无法使静态getter setter属性正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆