类中的Java枚举:使字段无法访问&方法可访问

时间:2017-06-06 21:09:37

标签: java enums

我有一个带有枚举的User类:

public class User
{
    private int index;
    private enum Counter
    {
        INSTANCE;
        private int objects;

        protected int getObjects()
        {
            return objects;
        }

        protected void setObjects()
        {
            this.objects++;
        }
    }

    public User(){
        this.index = User.Counter.INSTANCE.getObjects();
        User.Counter.INSTANCE.setObjects();
    }
}

在本课程中,我仍然能够做到

this.index = User.Counter.INSTANCE.objects; //in public User()

如何使这条线变得无效?

0 个答案:

没有答案