Constructor creates multiple variables, how to return them through other methods?

时光毁灭记忆、已成空白 提交于 2019-12-11 17:53:15

问题


public class Game {

    public Game(
        boolean createstage, //For sorting purposes
        int slength, 
        int sheight, 
        boolean createplayer, 
        int plength, 
        int pheight, 
        boolean playersprite,
        BufferedImage psprite, 
        boolean defaultcontrols,
        String pcontrols, 
        boolean test
        ) {
    if(test == true) { //if test is true, test
        new Test();
    }else{ //otherwise create a stage is createstage is true and
        if(createstage == true) {
            StageObj gamestage = new StageObj(slength, sheight);
        }
        if(createplayer==true) {
            PlayerObj player = new PlayerObj(plength, pheight, psprite, pcontrols);
        }
    }
}

public Game() {
    new StageObj(100, 100);
    new PlayerObj(10, 10);
}

public StageObj givestageobj() {
    return gamestage;
}

public PlayerObj giveplayerobj() {
    return player;
}

}

So goes the code of my constructor and two variables designed to return the variables created in the constructor. The problem is that the method giveplayerobj and givestageobj both don't find the variables gamestage and player. This makes sense, but how do I create the variables in the constructor and then somehow pass them to the giveplayerobj() and the givestageobj() variables so someone could theoretically go Game.giveplayerobj() which returns the playerobj created in the constructor?

Thanks

-JXP


回答1:


You need to declare them as class attributes and not inside the constructor for that to work. So, you code should look like what is shown below.

Changes:

  1. I have added two class variables StageObj andPlayerObj because you have getters for them in place.
  2. Removed the declaration of these two variables from inside the constructor.
  3. Added assignment to the overidden default constructor. (Probably what you were trying to achieve with the default constructor)

    public class Game {
    
    private StageObj gamestage = null;
    private PlayerObj player = null;
    
    public Game(
        boolean createstage, //For sorting purposes
        int slength, 
        int sheight, 
        boolean createplayer, 
        int plength, 
        int pheight, 
        boolean playersprite,
        BufferedImage psprite, 
        boolean defaultcontrols,
        String pcontrols, 
        boolean test
        ) {
    if(test == true) { //if test is true, test
        new Test();
    }else{ //otherwise create a stage is createstage is true and
        if(createstage == true) {
            gamestage = new StageObj(slength, sheight);
        }
        if(createplayer==true) {
            player = new PlayerObj(plength, pheight, psprite, pcontrols);
        }
    }
    }
    
    public Game() {
        gamestage = new StageObj(100, 100);
        player = new PlayerObj(10, 10);
    }
    
    public StageObj givestageobj() {
        return gamestage;
    }
    
    public PlayerObj giveplayerobj() {
        return player;
    }
    
    }
    



回答2:


Both variables gamestage and player need to be instance variables :

public class Game {
     private StageObj gamestage;
     private PlayerObj player;

(...) - and in the constructor :

        if(createstage == true) {
            gamestage = new StageObj(slength, sheight);
        }
        if(createplayer==true) {
            player = new PlayerObj(plength, pheight, psprite, pcontrols);
        }

(...)

}



回答3:


I think what you want is an instance variable, i.e.

public class Game {
  private StageObj gamestage;
  ...
  public Game(...) { ... }      

  public StageObj givestageobj() {
    return gamestage;
  }
}



回答4:


You need to set your datafields before the constructor and make getter en setter methods like this:

public class Game {

private StageObj gamestage = null;
private PlayerObj player = null;

public Game(
    boolean createstage, //For sorting purposes
    int slength, 
    int sheight, 
    boolean createplayer, 
    int plength, 
    int pheight, 
    boolean playersprite,
    BufferedImage psprite, 
    boolean defaultcontrols,
    String pcontrols, 
    boolean test
    ) {
if(test == true) { //if test is true, test
    new Test();
}else{ //otherwise create a stage is createstage is true and
    if(createstage == true) {
        gamestage = new StageObj(slength, sheight);
    }
    if(createplayer==true) {
        player = new PlayerObj(plength, pheight, psprite, pcontrols);
    }
}
}

public Game() {
    gamestage = new StageObj(100, 100);
    player = new PlayerObj(10, 10);
}

public StageObj givestageobj() {
    return gamestage;
}

public PlayerObj giveplayerobj() {
    return player;
}

}


public Game() {
new StageObj(100, 100);
new PlayerObj(10, 10);
}

public StageObj givestageobj() {
return gamestage;
}

public PlayerObj giveplayerobj() {
return player;
}

public int getSlenght(){
return slenght;
}

public void setSlenght(int slenght){
this.slenght = slenght;
}

}

And so on of course you need to add other classes, and not do everything in one class. You need a Player and Stage class ;-) Also take a look at the Java Lessons from Oracle http://docs.oracle.com/javase/tutorial/java/javaOO/



来源:https://stackoverflow.com/questions/9840480/constructor-creates-multiple-variables-how-to-return-them-through-other-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!