简体   繁体   English

如何从另一个Java类更改变量的值?

[英]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. 有两种方法可以解决此问题。

  1. Best solution is to pass the class object (as parameter) to the third class where you are calling the 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());
    }
}
  1. If you want share the variable 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.

相关问题 如何在另一个类中更改变量的值,并且仍然能够在另一个类中更改它的值? - How do I change the value of a variable from another class and still be able to change it within the other class? 如何在一个Java类中更改另一个Java类中的变量,然后从第三个Java类访问该更改的变量? - How do I change a variable in one java class from another, then access that changed variable from a third java class? 如何从另一个类更改或分配给私有变量JTextField的值? - How do I Change or Assign a Value to a Private Variable JTextField from another class? 如何在Java中更改其他类的变量? - How do I change a variable from a different class in Java? 如何在 Java 类中更改变量 - How do I change a Variable, in a Java Class 我如何更改其他类中的变量,该变量将更改Java所有类中的值 - how do i change a variable in an others class that will change its value in all class in java 如何使用 java 中的另一个 class 更改 class 中的变量值? - How to change the value of variable inside a class using another class in java? 如何根据 POJO 中的另一个值更改变量的值? - How do I change the value of a variable based on another value in POJO? Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class? 如何从另一个类将变量集的值更改为null? - How to change the value of a variable set to null from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM