【问题标题】:Constructor creates multiple variables, how to return them through other methods?构造函数创建多个变量,如何通过其他方法返回?
【发布时间】:2012-03-23 13:59:59
【问题描述】:
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?

谢谢

-JXP

【问题讨论】:

  • 添加 StageObj gamestage = null;作为实例变量并删除构造函数中的局部变量创建。 if(createstage == true) { gamestage = new StageObj(slength, sheight); }
  • 你的命名习惯太糟糕了,用 Obj 后缀是一个完整的tautology
  • 无论@JarrodRoberson 说什么都是正确的。请使用更好的命名约定。它使代码具有可读性和意义。
  • @JarrodRoberson 哈哈,对不起我可怕的命名约定

标签: java variables constructor


【解决方案1】:

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

变化:

  1. 我添加了两个类变量 StageObj 和PlayerObj,因为您已经为它们准备好了吸气剂。
  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】:

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

(...)

}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      你需要在构造函数之前设置你的数据字段,并像这样使用 getter en 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/

      【讨论】:

      • 有了这个命名约定,我们真的可以决定构造函数中的所有参数是否都应该是这个类的属性吗?我认为这些最好是播放器和舞台对象的属性 op 试图返回那里。不是吗?
      • 是的,当然是!这只是 getter 和 setter 的一个例子。他需要制作很多其他的对象类,否则他会将所有的逻辑放在一个类中,这是不对的!这只是一个小例子;-)
      • 我仍然认为它们是 Player 和 stage 类的属性。这两个对象是在这里创建的,这就是组合。没关系,还不错。但是,plength、pheight 和 pcontrols 更适合作为 Player 的属性。 Stage 的长度和高度也是如此。
      猜你喜欢
      • 2016-09-24
      • 2011-03-03
      • 1970-01-01
      • 2022-01-03
      • 2016-04-08
      • 2018-12-21
      • 1970-01-01
      • 2015-05-06
      • 2017-04-25
      相关资源
      最近更新 更多