构造函数创建多个变量,如何通过其他方法返回它们? [英] Constructor creates multiple variables, how to return them through other methods?

查看:150
本文介绍了构造函数创建多个变量,如何通过其他方法返回它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

感谢

-JXP

推荐答案

您需要将它们声明为类属性,而不是在构造函数中声明它们。因此,您的代码应该如下所示。

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.

更改:


  1. 我已经添加了两个类变量StageObj和PlayerObj,因为你有它们的getters。

  2. 从构造函数中删除了这两个变量的声明。

  3. 为覆盖的默认构造函数添加了分配。 (可能是你试图用默认构造函数实现的)

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

}


这篇关于构造函数创建多个变量,如何通过其他方法返回它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆