简体   繁体   English

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

[英]How do you pass an updated global class variable from one method into another?

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,它仍似乎无法正常工作

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()

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类的对象,应将其声明为static,如下所示:

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: 因此,在主要情况下,您甚至不必制作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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中将变量从一类传递到另一类? - How Do You Pass a Variable from One Class to Another in Java? 如何将全局变量从一个测试类传递到另一个测试类并在 selenium 的下拉列表中选择该值? - How to pass the global variable from one Test class to another test class and select that value in a dropdown in selenium? 如何从一个方法,另一个类的变量数据中读取公共静态变量并获取更新的数据 - How do I read in a public static, with variable data from a method, from another class and get updated data 如何将一类的主方法中定义的变量传递给另一类? - How do you transfer a variable defined in the main method of one class to another? 如何将字符串变量 java 从一种方法传递到另一种方法 - How pass string variable java from one method to another method 如何将变量从一个 class 传递到另一个 class? - How to pass variable from one class to another class? 如何将值从一个 class 的 onOptionsItemSelected 方法传递到另一个 class - How to pass value from onOptionsItemSelected method of one class to another class 如何创建实例变量以从另一个 class 调用非静态方法? - How do you create an instance variable to call a non-static method from another class? 我如何通过 ArrayList<string> 在 Java 中从一种方法到另一种方法。 变量未初始化错误</string> - How do I pass an ArrayList<String> from one method to another in Java. Variable not initialised error Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM