在 Java 中设置来自另一个类的变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24433951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the value of a variable from another class in Java
提问by SAF
I was wondering if it is possible to reset the value of a variable from another class. For example I have this variable in a HillClimber (hc) class:
我想知道是否可以从另一个类重置变量的值。例如,我在 HillClimber (hc) 类中有这个变量:
public int ballWeight = 200;
What I want to do is run a simulation of a game with the ball weighting at this value. When it is finished I want to set the value to 201 from another class and begin the simulation again, and after that increase to 202 and start another and so on. My problem is that every time I restart the simulation the ballWeight variable is reset to 200. I have tried using a setter method in the HillClimber class:
我想要做的是在这个值下运行一个球权重的游戏模拟。完成后,我想将另一个类的值设置为 201 并再次开始模拟,然后增加到 202 并开始另一个类,依此类推。我的问题是每次重新启动模拟时,ballWeight 变量都会重置为 200。我尝试在 HillClimber 类中使用 setter 方法:
public int setBallWeight(int ballWeight) {
return this.ballWeight = ballWeight;
}
and called it from another class at the end of a simulation:
并在模拟结束时从另一个类调用它:
hc.setBallWeight(hc.ballWeight+1);
but this does not seem to work as the variables stored value is not changed. Does anyone know how I can do this so the stored value of ballWeight will be increased by 1 each time a simulation ends? Or is this even possible? Thanks.
但这似乎不起作用,因为变量存储的值没有改变。有谁知道我如何做到这一点,以便每次模拟结束时 ballWeight 的存储值都会增加 1?或者这甚至可能吗?谢谢。
采纳答案by Narmer
Usually in a POJO
you have what are called a getter and a setter method for every variable of the object. In your case:
通常在 a 中,POJO
您对对象的每个变量都有所谓的 getter 和 setter 方法。在你的情况下:
public class HillClimber{
private int ballWeight;
public HillClimber(){
this.ballWeight = 200;
}
public void setBallWeight(int ballWeight){
this.ballWeight = ballWeight;
}
public int getBallWeight(){
return this.ballWeight;
}
}
In this way you can access the variable ballWeight
via get and set method. You don't access it directly like in hc.ballWeight
, which is possible but is a bad practice, and prevent this access type declaring your variable as private (meaning that only the class in which it is declared can directly access it).
通过这种方式,您可以ballWeight
通过 get 和 set 方法访问变量。您不像 in 那样直接访问它hc.ballWeight
,这是可能的,但这是一种不好的做法,并防止此访问类型将您的变量声明为私有(意味着只有声明它的类才能直接访问它)。
To fullfill your request of adding one at every run of the game you can therefore call
为了满足您在每次游戏运行时添加一个的请求,您可以调用
hc.setBallWeight(++hc.getBallWeight()); //Equivalent to hc.setBallWeight(hc.getBallWeight() + 1);
I usually don't use this approach if the class isn't automatically generated (as in an Hibernate context), but instead declare another method in the HillClimber
class
如果类不是自动生成的(如在 Hibernate 上下文中),我通常不使用这种方法,而是在HillClimber
类中声明另一个方法
public void incrementBallWeight(int ballWeightToAdd){
this.ballWeight += ballweiGhtToAdd; //Equivalent to this.ballWeight = this.ballWeight + ballweiGhtToAdd;
}
or if I always need to add only one to my variable
或者如果我总是只需要向我的变量添加一个
public void incrementBallWeight(){
this.ballWeight++;
}
and then simply call incrementBallWeight
after every game run.
然后incrementBallWeight
在每次游戏运行后简单地调用。
NB:to have this working you will have to use always the same instance of HillClimber
. In your main
注意:要使此工作正常运行,您必须始终使用相同的HillClimber
. 在你的主要
public class Game{
private HillClimber hc = new HillClimber(); //Create the instance and sets ballWeight to 200
public static void main(String[] args){
playGame();
hc.incrementBallWeight(); //ballWeight == 201
playAnotherGame()
hc.incrementBallWeight(); //ballWeight == 202 -> Always the same instance of HillClimber (hc)
.
.
.
}
}
EDIT
编辑
I think your problem is greater than that. You are asking to save the state of a variable ( meaning that this value should be available also if you turn off and on your pc) without using a permanent storage. This is simply unachievable.
我认为你的问题比这更大。您要求在不使用永久存储的情况下保存变量的状态(这意味着如果您关闭和打开电脑,该值也应该可用)。这是根本无法实现的。
You should rethink your program (and I mean java program, not a "game run") to not stop after every game run. You can do this in different ways: via Swing GUI
, via user input from stdin
and so on. If you want some help on this topic, we need to know more of your code (maybe putting the whole of it is best).
您应该重新考虑您的程序(我的意思是 Java 程序,而不是“游戏运行”),不要在每次游戏运行后停止。您可以通过不同的方式做到这一点:通过Swing GUI
、通过用户输入stdin
等等。如果您需要有关此主题的一些帮助,我们需要了解更多您的代码(也许最好将其全部放入)。
OR you can use a file to store your value, which is not as difficult as you think. (Also).
或者你可以使用一个文件来存储你的值,这并不像你想象的那么困难。(也)。