如何将更新的全局类变量从一个方法传递到另一个方法?

时间:2022-05-31 23:09:29

I update a variable (which is global in the class) in one method and I cannot seem to be able to then pass that updated variable into another method. Any help would be appreciated, thank you. Here's my shortened code:

我在一个方法中更新一个变量(在类中是全局的),然后我似乎无法将该更新的变量传递给另一个方法。任何帮助将不胜感激,谢谢。这是我缩短的代码:

public class Game{

    private int randomIndexX;

    protected String spawn(){
        randomIndexX = randomGenerator.nextInt(10);
        return null;
    }

    protected String test(){
        System.out.println(this.randomIndexX);
        return null;
    }
}


public class Player extends Game{
    protected String getNextAction(String command) {
        switch(command){
            case "test":
                test();
                break;
        }
    }
}


public static void main(String[] args) {
    Game game = new Game();
    Player player = new Player();

    game.spawn();
    player.getInputFromConsole();
}

EDIT: so when i call test() from the Player class i want it to print out randomIndexX but it still doesn't seem to be working even with this.randomIndexX in the method test()

编辑:所以当我从Player类调用test()时,我希望它打印出randomIndexX,但它仍然似乎在方法test()中使用this.randomIndexX似乎没有工作

2 个解决方案

#1


0  

EDIT: so when i call test() from the Player class i want it to print out randomIndexX but it still doesn't seem to be working even with this.randomIndexX in the method test().

编辑:所以当我从Player类调用test()时,我希望它打印出randomIndexX,但它仍然似乎在方法test()中使用this.randomIndexX似乎没有工作。

So test() is instance method, which means you'll have to make an instance of Class Game in order to call that method, your randomIndexX is instance member so you need to think well what you want to do, IF randomIndexX is common for all the objects of Game class, you should declare it static as in:

所以test()是实例方法,这意味着你必须创建一个Class Game实例才能调用该方法,你的randomIndexX是实例成员所以你需要好好想想你想做什么,如果randomIndexX是常见的Game类的所有对象,你应该将它声明为静态,如:

private static in randomIndexX;

As it's value won't change depending on an object instance.

因为它的值不会根据对象实例而改变。

So in order to access that variable from outside of the class since it's private you declare a public method to retrieve that value (getter or also known as accessor):

因此,为了从类外部访问该变量,因为它是私有的,您声明一个公共方法来检索该值(getter或也称为访问器):

public static int getRandomIndex(){
    return randomIndexX;
}

So when in main, you don't even have to make an instance of the Game class to access value that's being held in randomIndexX, you just call the getter method like this:

因此,当你在main中,你甚至不必创建一个Game类的实例来访问randomIndexX中保存的值时,你只需要调用这样的getter方法:

System.out.println(Game.getRandomIndex());

The line above will print 0 to the console as 0 is default value for members of type int, now if you want to be able to change it, you just make a setter or mutator method in Game class as well:

上面的行将0输出到控制台,因为0是int类型成员的默认值,现在如果你想能够改变它,你只需在Game类中创建一个setter或mutator方法:

public static void setRandomIndex(int n){
   randomIndexX = n;
}

And there you go, you can now set and retrieve "randomIndexX" field from outside of the Game class.

你去了,你现在可以从Game类的外部设置和检索“randomIndexX”字段。

For example, the code below will set value of randomIndexX to 5 and then print it in the console:

例如,下面的代码将randomIndexX的值设置为5,然后在控制台中打印它:

Game.setRandomIndex(5);
System.out.println(Game.getRandomIndex());

#2


0  

The first problem I can see is that you don't have a constructor.(Optional) (If you don't make one the compiler makes what is called a "Default" constructor which is a constructor without any parameters. Its usually good practice to explicitly create a class constructor.

我可以看到的第一个问题是你没有构造函数。(可选)(如果你没有编译器,编译器会生成所谓的“默认”构造函数,它是一个没有任何参数的构造函数。它通常是一个很好的实践显式创建一个类构造函数。

The second problem I can see is that you missing the end bracket. Fix shown below.

我能看到的第二个问题是你错过了结束括号。修复如下所示。

public class Game

{

{

private int randomIndexX;

protected String spawn()
{
    randomIndexX = 0;
    return null;
}

protected String test()
{
    System.out.println(randomIndexX);
    return null;
}

}

}

You can construct it and trigger any methods you wish:

您可以构建它并触发您希望的任何方法:

Game game = new Game();
game.spawn();
game.test()

#1


0  

EDIT: so when i call test() from the Player class i want it to print out randomIndexX but it still doesn't seem to be working even with this.randomIndexX in the method test().

编辑:所以当我从Player类调用test()时,我希望它打印出randomIndexX,但它仍然似乎在方法test()中使用this.randomIndexX似乎没有工作。

So test() is instance method, which means you'll have to make an instance of Class Game in order to call that method, your randomIndexX is instance member so you need to think well what you want to do, IF randomIndexX is common for all the objects of Game class, you should declare it static as in:

所以test()是实例方法,这意味着你必须创建一个Class Game实例才能调用该方法,你的randomIndexX是实例成员所以你需要好好想想你想做什么,如果randomIndexX是常见的Game类的所有对象,你应该将它声明为静态,如:

private static in randomIndexX;

As it's value won't change depending on an object instance.

因为它的值不会根据对象实例而改变。

So in order to access that variable from outside of the class since it's private you declare a public method to retrieve that value (getter or also known as accessor):

因此,为了从类外部访问该变量,因为它是私有的,您声明一个公共方法来检索该值(getter或也称为访问器):

public static int getRandomIndex(){
    return randomIndexX;
}

So when in main, you don't even have to make an instance of the Game class to access value that's being held in randomIndexX, you just call the getter method like this:

因此,当你在main中,你甚至不必创建一个Game类的实例来访问randomIndexX中保存的值时,你只需要调用这样的getter方法:

System.out.println(Game.getRandomIndex());

The line above will print 0 to the console as 0 is default value for members of type int, now if you want to be able to change it, you just make a setter or mutator method in Game class as well:

上面的行将0输出到控制台,因为0是int类型成员的默认值,现在如果你想能够改变它,你只需在Game类中创建一个setter或mutator方法:

public static void setRandomIndex(int n){
   randomIndexX = n;
}

And there you go, you can now set and retrieve "randomIndexX" field from outside of the Game class.

你去了,你现在可以从Game类的外部设置和检索“randomIndexX”字段。

For example, the code below will set value of randomIndexX to 5 and then print it in the console:

例如,下面的代码将randomIndexX的值设置为5,然后在控制台中打印它:

Game.setRandomIndex(5);
System.out.println(Game.getRandomIndex());

#2


0  

The first problem I can see is that you don't have a constructor.(Optional) (If you don't make one the compiler makes what is called a "Default" constructor which is a constructor without any parameters. Its usually good practice to explicitly create a class constructor.

我可以看到的第一个问题是你没有构造函数。(可选)(如果你没有编译器,编译器会生成所谓的“默认”构造函数,它是一个没有任何参数的构造函数。它通常是一个很好的实践显式创建一个类构造函数。

The second problem I can see is that you missing the end bracket. Fix shown below.

我能看到的第二个问题是你错过了结束括号。修复如下所示。

public class Game

{

{

private int randomIndexX;

protected String spawn()
{
    randomIndexX = 0;
    return null;
}

protected String test()
{
    System.out.println(randomIndexX);
    return null;
}

}

}

You can construct it and trigger any methods you wish:

您可以构建它并触发您希望的任何方法:

Game game = new Game();
game.spawn();
game.test()