简体   繁体   English

我如何从 Java 中的不同类访问变量?

[英]How Would I get access to a variable from a different class in java?

I currently have a variable that I would like to access in a different class but i'm not sure on how I would go about doing so.我目前有一个变量,我想在不同的类中访问它,但我不确定我将如何去做。 I have tried doing stuff like int nums = SizeSelect.amount;我试过做像int nums = SizeSelect.amount;这样的东西int nums = SizeSelect.amount; but that has not worked.但这并没有奏效。 Here is the code from the class I'm trying to access the amount varaible.这是我试图访问数量变量的类中的代码。

public class SizeSelect extends BorderPane {
    public SizeSelect(){
        size8.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            int amount = 9;
        });

        size32.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            int amount = 33;
        });

        size16.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            int amount = 17;
        });

and here is the code that I'm trying to access that amount varible这是我试图访问该金额变量的代码

public class EnterNames extends BorderPane {
    public EnterNames(){
        int nums = SizeSelect.amount;
        for (int j = 0; j < nums; j++) {
            int teamNum = j + 1;
            TextField test = new TextField();
            test.setPromptText("Enter Team " + teamNum);
            box.getChildren().add(test);
            test.setMaxWidth(500);
        }

Your question needs some information on when and where EnterNames is declared, but assuming it is done in EnterNamesScenes , then one solution would be to first add amount as a EnterNamesScenes attribute, and then pass directly the value from it as a constructor argument (in SizeSelect ) and then use it to construct a EnterNames instance.您的问题需要一些关于何时何地声明EnterNames信息,但假设它是在EnterNamesScenes完成的,那么一种解决方案是首先将amount添加为EnterNamesScenes属性,然后直接将它的值作为构造函数参数传递(在SizeSelect ) 然后用它来构造一个EnterNames实例。

For example :例如 :

size16.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene(17));
}

And in EnterNamesScene :EnterNamesScene

private int amount;
public EnterNamesScene(int amount){
    this.amount=amount; //this value comes from the button action and will be saved to create a EnterName

    //Constructor stuff
}

Then whenever you want to create a EnterNames instance, simply add the value of amount as an argument :然后,每当您想创建EnterNames实例时,只需将amount的值添加为参数:

EnterNames en = new EnterNames(this.amount);

And the constructor :和构造函数:

public EnterNames(int amount){
    for (int j = 0; j < amount; j++) {
    int teamNum = j + 1;
    // Rest of the code
}

NOTE : As I said before, this will only work in the case where EnterNames is created inside EnterNamesScenes .注意:正如我之前所说,这只适用于在EnterNames中创建EnterNamesScenes Otherwise, you will have to provide more information about how EnterNames is instantiated.否则,您将必须提供有关如何实例化EnterNames更多信息。

The amount variable in SizeSelect is local to each onClicked handler, and thus not reachable outside the handler function scope. SizeSelect 中的 amount 变量对于每个 onClicked 处理程序都是本地的,因此在处理程序函数范围之外无法访问。

If you want to be able to reach SizeSelect.amount, you need to declare it as static in the class.如果您希望能够达到 SizeSelect.amount,则需要在类中将其声明为静态。

public class SizeSelect extends BorderPane {
    public static int amount;

    public SizeSelect(){
        size8.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            amount = 9;
        });

        size32.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            amount = 33;
        });

        size16.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            amount = 17;
        });

Note that this is a quick fix, the more appropriate solution would be to use a getter/setter.请注意,这是一个快速修复,更合适的解决方案是使用 getter/setter。 See https://www.w3schools.com/java/java_encapsulation.asphttps://www.w3schools.com/java/java_encapsulation.asp

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM