【发布时间】: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