构造函数创建多个变量,如何通过其他方法返回它们?

时间:2012-03-23 14:00:00

标签: java variables constructor

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;
}

}

所以我的构造函数的代码和两个变量设计用于返回在构造函数中创建的变量。问题是,方法giveplayerobj和givestageobj都没有找到变量gamestage和player。这是有道理的,但是我如何在构造函数中创建变量然后以某种方式将它们传递给giveplayerobj()和givestageobj()变量,这样理论上有人可以去Game.giveplayerobj()它返回在构造函数中创建的playerobj? / p>

由于

-JXP

4 个答案:

答案 0 :(得分:1)

变量gamestageplayer都需要是实例变量:

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);
        }

(...)

}

答案 1 :(得分:1)

您需要将它们声明为类属性,而不是在构造函数中声明它才能工作。所以,你的代码看起来应该如下所示。

的变化:

  1. 我添加了两个类变量StageObj和LayerObj,因为你已经为它们准备了getter。
  2. 从构造函数中删除了这两个变量的声明。
  3. 为已覆盖的默认构造函数添加了赋值。 (可能是你试图用默认构造函数实现的)

    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 :(得分:0)

我认为你想要的是一个实例变量,即

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

  public StageObj givestageobj() {
    return gamestage;
  }
}

答案 3 :(得分:-1)

您需要在构造函数之前设置数据域,并使getter和setter方法如下:

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;
}

}

等等当然你需要添加其他类,而不是在一个类中做所有事情。你需要一个Player和Stage类;-)另外看一下来自Oracle的Java课程http://docs.oracle.com/javase/tutorial/java/javaOO/