我知道这个问题出现的时候,但即使看着问问题的后,我无法找到一个解决方案中引用..Zuul的世界:非静态方法getCurrentRoom()不能从静态上下文
所以对于学校来说,我们必须创建自己的Zuul世界版本。我已经在其中实施了投影仪。首先我把它放在游戏课上,但是我决定把它放在自己的班级Beamer中会更好。在默类看起来如下:
public class Beamer
{
private Room beamerRoom;
private Room beamer;
private int timesFired;
private boolean beamerCharged;
public Beamer(int timesFired, boolean beamerCharged)
{
this.timesFired = 0;
this.beamerCharged = false;
}
public int getTimesFired()
{
return timesFired;
}
public Room getBeamerRoom()
{
return beamerRoom;
}
public boolean getBeamerCharged()
{
return beamerCharged;
}
public Room setBeamerRoom(Room room)
{
this.beamerRoom = Game.getCurrentRoom();
return beamerRoom;
}
/**
* Try to use beamer device. When you charge the beamer, it memorizes the current room.
* When you fire the beamer, it transports you immediately back to the room it was
* charged in.
*/
public void beamer(Command command){
if (!command.hasSecondWord()) {
// if there is no second word, we don't know what to do...
System.out.println("Charge or fire beamer?");
return;
}
String action = command.getSecondWord();
if (action.equals("charge")) {
if(timesFired() < 1)
{
beamerRoom = Game.getCurrentRoom();
System.out.println("This room is charged to beam!");
beamerCharged = true;
}
else
{
System.out.println("The beamer has already been fired and can't be charged again");
}
} else if (action.equals("fire")) {
if(beamerCharged == true)
{
if(timesFired < 1)
{
Game.getCurrentRoom() = beamer.getBeamerRoom();
System.out.println("The beamer has succesfully been fired!");
System.out.println(currentRoom.getLongDescription());
timesFired++;
beamerCharged = false;
}
else
{
System.out.println("You can only fire the beamer once!");
}
}
else
{
System.out.println("The beamer hasn't been charged yet!");
}
} else {
System.out.println("Invalid beamer command!");
return;
}
}
}
我得到的错误method getCurrentRoom() cannot be referenced from a static context
这里: beamerRoom = Game.getCurrentRoom();
游戏中的方法来获得当前室内如下:
public Room getCurrentRoom()
{
return currentRoom;
}
很简单,这在我的脑海里应该工作。
我该如何解决这个问题?我环顾四周,但无法找到有效的解决方法。请参阅
如果您需要更多的游戏类代码,请询问。我没有发布它,因为它有300多行。编辑: 我已经找到了我做错了什么。通过使用Game.getCurrentRoom
它看着类游戏,而不是游戏的对象。至少这是我认为出错的原因。我有更多的方法,使用大写字母(Game.<method>
),错误消息,Player.<method>
),但是当我没有使用大写字母(game.<method>
),player.<method>
)它工作正常。
所以我想问题解决了。
如果您认为它应该起作用,您需要回到其他问题以了解其原因。你对静态的含义有多少了解?考虑到每个'Game'实例可以有不同的“当前房间”,*您正在尝试使用哪个*'Game'目前的房间? –