簡體   English   中英

如何在不創建對象的情況下從另一個類獲取變量

[英]How to get a variable from another class without creating an object

我正在用Java創建風險游戲,需要圖形方面的幫助。

我有3個課程,第一個是主要課程,它將啟動第二個課程:

public class Main extends StateBasedGame{

public static final String nomjeu = "Risk !"; //name of the game
public static final int nombre_joueurs = 0;
public static final int nom_joueurs = 1;

public Main(String nomjeu) {

    super(nomjeu);
    this.addState(new NombreJoueurs(nombre_joueurs));
    this.addState(new NomJoueurs(nom_joueurs));
}

public void initStatesList(GameContainer container) throws SlickException{

    this.getState(nombre_joueurs).init(container, this);
    this.getState(nom_joueurs).init(container, this);
    this.enterState(nombre_joueurs);

}

public static void main(String[] args) {

    AppGameContainer appgc;
    try {

        appgc = new AppGameContainer(new Main(nomjeu));
        appgc.setDisplayMode(1920/2,1080/2, false);
        appgc.start();

    }catch(SlickException e) {
        e.printStackTrace();
    }

}

第二個是該類,我在其中顯示5個5個按鈕,詢問用戶要玩多少游戲(從2到6):

public class NombreJoueurs extends BasicGameState{


int nb_joueurs; //number of players
Image bouton_2_joueurs;
Image bouton_3_joueurs;
Image bouton_4_joueurs;  
Image bouton_5_joueurs;
Image bouton_6_joueurs;

public NombreJoueurs(int slate) {

}


public void init(GameContainer container, StateBasedGame sbg) throws SlickException {

    bouton_2_joueurs = new Image("res/button_2_joueurs.png");
    bouton_3_joueurs = new Image("res/button_3_joueurs.png");
    bouton_4_joueurs = new Image("res/button_4_joueurs.png");
    bouton_5_joueurs = new Image("res/button_5_joueurs.png");
    bouton_6_joueurs = new Image("res/button_6_joueurs.png");
    nb_joueurs = 0;

}

public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {

    int x = Mouse.getX();
    int y = Mouse.getY();

    // 2 Players
    if( (x>423 && y>400) && (x<567 && y<440) ) {

        if (Mouse.isButtonDown(0)) {

            nb_joueurs = 2;
            sbg.enterState(1);


        }

    }
    // 3 Players
    if( (x>423 && y>330) && (x<567 && y<370) ) {

        if (Mouse.isButtonDown(0)) {

            nb_joueurs = 3;
            sbg.enterState(1);

        }

    }
    // 4 Players
    if( (x>423 && y>260) && (x<567 && y<300) ) {

        if (Mouse.isButtonDown(0)) {

            nb_joueurs = 4;
            sbg.enterState(1);

        }

    }
    // 5 Players
    if( (x>423 && y>190) && (x<567 && y<230) ) {

        if (Mouse.isButtonDown(0)) {

            nb_joueurs = 5;
            sbg.enterState(1);

        }

    }
    // 6 Players
    if( (x>423 && y>120) && (x<567 && y<160) ) {

        if (Mouse.isButtonDown(0)) {

            nb_joueurs = 6;
            sbg.enterState(1);

        }

    }

}

public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {

    g.drawString("Combien de joueurs ?", 405, 50);
    bouton_2_joueurs.draw(420,100);
    bouton_3_joueurs.draw(420,170);
    bouton_4_joueurs.draw(420,240);
    bouton_5_joueurs.draw(420,310);
    bouton_6_joueurs.draw(420,380);

}





public int getID() {
    return 0;
}

例如,如果他單擊“ 2個玩家”按鈕,我將把nb_joueurs的值設置為2並啟動我的第三堂課,我要求他輸入玩家的名字:

public class NomJoueurs extends BasicGameState {

TextField nomj1;
TextField nomj2;
TextField nomj3;
TextField nomj4;
TextField nomj5;
TextField nomj6;



public NomJoueurs(int state) {

}


public void init(GameContainer container, StateBasedGame sbg) throws SlickException {

    // Here there should be the code where I create the textfields, but I need to know how many players
    // are playing to create the right amount of textfields.
    // So I need to get the variable nombre_joueurs

}

public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {


}


public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {

    g.drawString("Quels sont les noms des joueurs ?", 330, 50);

}


public int getID() {
    return 1;
}

我需要在第三堂課中獲取變量nb_joueurs。 我怎么做 ?

我已經嘗試過吸氣劑,但無法創建對象來獲取值。

如果是靜態的,則可以使用靜態吸氣劑,例如

private static String test;

public static String getTest() {
        return test;
    }

    public static void setTest(String test) {
        Main.test = test;
    }

除了使其靜態化之外,如果未加載該類,請確保實例化該類。 通過使用

Class.forName(NombreJoueurs.class.getCanonicalName());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM