问题
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;
}
}
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?
Thanks
-JXP
回答1:
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.
Changes:
- I have added two class variables StageObj andPlayerObj because you have getters for them in place.
- Removed the declaration of these two variables from inside the constructor.
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; } }
回答2:
Both variables gamestage and player need to be instance variables :
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:
I think what you want is an instance variable, i.e.
public class Game {
private StageObj gamestage;
...
public Game(...) { ... }
public StageObj givestageobj() {
return gamestage;
}
}
回答4:
You need to set your datafields before the constructor and make getter en setter methods like this:
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;
}
}
And so on of course you need to add other classes, and not do everything in one class. You need a Player and Stage class ;-) Also take a look at the Java Lessons from Oracle http://docs.oracle.com/javase/tutorial/java/javaOO/
来源:https://stackoverflow.com/questions/9840480/constructor-creates-multiple-variables-how-to-return-them-through-other-methods