Java“获取"和“设置"方法 [英] Java "Get" and "Set" Methods
问题描述
我有两个类,在类Player
中,我有定义为private int collectedDots
的变量,我想在另一个类Exit
中进行访问.
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
.
我已经在Player类中定义了Get and Set方法,
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;
}
但是现在我想从Exit
类访问collectedDots
字段.当我将这两个方法复制到Exit类时,我一直收到错误cannot find symbol - variable 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
.
据我了解,一旦我设置了get
和set
方法,便可以从Player类中检索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?
推荐答案
我对Java尚不熟悉,但是我想您需要确保已导入要使用的其他类,或者您可以类的实例:
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点=新的collectdDots(); int dotsValue = dots.getCollectedDots();
collectedDots dots = new collectedDots(); int dotsValue = dots.getCollectedDots();
这篇关于Java“获取"和“设置"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!