[英]How do i change value of variable from another java class?
I am new to java so help is much appreciated. 我是Java新手,所以非常感谢您的帮助。
I have a few classes. 我有几节课。 I want to pass the user input from my GUI class to my Basket class where it will update the value stored in the variable. 我想将用户输入从我的GUI类传递到我的Basket类,在其中它将更新存储在变量中的值。 Below is what i currently have but it doesn't seem to be updating the variable. 下面是我目前拥有的东西,但似乎没有更新变量。
Class One - GUI Class: I would like to pass the 'seats' to the basket class 第一课-GUI课:我想将“座位”传递给购物篮课
Basket b;
b = new Basket();
String seats = JOptionPane.showInputDialog("Enter the number of seats to book");
try {
int currentValue = Integer.parseInt(seats);
int newValue = currentValue;
b.setSeatsBooked(newValue);
//some code emitted
}
Second Class - Basket Class, I would like the 'seats' to be passed into this class and stored in the instance variable. 第二类-篮子类,我希望将“座位”传递给此类并存储在实例变量中。
public class Basket {
private int seatsBooked;
public int getSeatsBooked() {
return seatsBooked;
}
public void setSeatsBooked(int seatsBooked) {
this.seatsBooked = seatsBooked;
}
}
This is in another class where i see the result as 0: 这是在另一个班级中,我将结果视为0:
Basket b;
b = new Basket();
lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());
This is the reason of the wrongly behaviour: 这是错误行为的原因:
Basket b;
b = new Basket();
lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());
you are creating a new object! 您正在创建一个新对象! so the default value is zero for that 因此默认值为零
You need to pass the class object for which you have stored the value. 您需要传递已为其存储值的类对象。 If you create a new instance of a class, how can you expect that object will contain that value? 如果创建类的新实例,您如何期望该对象包含该值?
In the following two cases, 在以下两种情况下,
Basket b;
b = new Basket();
String seats = JOptionPane.showInputDialog("Enter the number of seats to book");
try {
int currentValue = Integer.parseInt(seats);
int newValue = currentValue;
b.setSeatsBooked(newValue);
//some code emitted
}
and 和
Basket b;
b = new Basket();
lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());
Are the object b
of class Basket
same in both blocks? 两个块中Basket
类的对象b
是否相同? Answer is No. So, you are getting zero value when you call b.getSeatsBooked()
from the newly created object b
of class Basket
in the second block of code. 答案是b.getSeatsBooked()
。因此,在第二个代码块中,从类别为Basket
的新创建的对象b
调用b.getSeatsBooked()
时,将获得零值。
There are two way to solve this problem. 有两种方法可以解决此问题。
getSeatsBooked()
method. 最好的解决方案是将类对象(作为参数)传递给您正在调用getSeatsBooked()
方法的第三类。 Example : Suppose your another class is X
and there is a function called method()
. 示例 :假设您的另一个类是X
并且有一个名为method()
的函数。 Then you can pass Basket
class object as parameter to that function where you can access it to get the updated value. 然后,您可以将Basket
类对象作为参数传递给该函数,在其中您可以访问它以获取更新的值。 The code may look like as follows. 该代码可能如下所示。
class X{
// other code
public void method(Basket b){
lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());
}
}
seatsBooked
across all class instances, you can declare the variable as static. 如果要在所有类实例之间共享变量seatsBooked
,则可以将变量声明为静态。 (I guess this is not what you are looking for) (我想这不是您想要的) Try making the variable static, an example for this would be: 尝试将变量设置为静态,例如:
public static int testInt = 0;
The way you would call this in the other class is up to you, there are 2 options that you can use. 在另一个类中调用此方法的方式由您决定,可以使用2个选项。
//NOT CLASS NAME IS FOR YOUR ACTUAL CLASS NAME.
ClassName varName = new ClassName();
varname.testInt;
//OR YOU CAN USE
ClassName.testInt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.