Java “Get” and “Set” Methods

徘徊边缘 提交于 2019-12-25 07:23:29

问题


I have two classes, in class Player i have the variable which is defined as private int collectedDots and I want to access in another class Exit.

I have defined the Get and Set method within class Player as such:

public void setCollectedDots(int cd)
{
    collectedDots = cd;
}

public int getCollectedDots()
{
    return collectedDots;
}

But now I want to access the collectedDots field from the Exit class. When I copy those two methods into the Exit class I keep getting the error cannot find symbol - variable collectedDots.

It was my understanding that I would then be able to retrieve the collectedDots variable from the Player class once I set the get and set methods..

Any ideas where i'm going wrong?


回答1:


I'm not yet that familiar with java but i guess you need to make sure that you have imported the other class that you would use or you can you an instance of a class:

collectedDots dots = new collectedDots(); int dotsValue = dots.getCollectedDots();




回答2:


You should use public int collectedDots variable rather than private int collectedDots variable



来源:https://stackoverflow.com/questions/23189672/java-get-and-set-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!