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;作爲實例變量,並刪除構造函數中的局部變量創建。如果(createstage == true){gamestage = new StageObj(slength,sheight); } – kosa 2012-03-23 14:04:33
你的命名約定是可怕的,後綴與Obj的一切是一個完整的[重言式](http://www.vertigrated.com/blog/2011/02/interface-and-class-naming-anti-patterns-java-命名-慣例,重言式/)! – 2012-03-23 14:06:22
不管@JarrodRoberson說的是什麼。請使用更好的命名約定。它使代碼可讀和有意義。 – r3st0r3 2012-03-23 14:20:47