2014-01-18 29 views
1

我知道這個問題出現的時候,但即使看着問問題的後,我無法找到一個解決方案中引用..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>)它工作正常。

所以我想問題解決了。

+1

如果您認爲它應該起作用,您需要回到其他問題以瞭解其原因。你對靜態的含義有多少了解?考慮到每個'Game'實例可以有不同的「當前房間」,*您正在嘗試使用哪個*'Game'目前的房間? –

回答

0

當您致電Game.getCurrentRoom()時,您正試圖調用類Game上的方法。但是您將該方法定義爲實例方法。因此,您需要更改代碼以使用Game的單例實例來訪問其實例方法,或者需要將Class上的mehtods聲明爲靜態。請注意,這些方法再次只能訪問類成員,而不是實例成員。

public static Room getCurrentRoom() 
{ 
    return currentRoom; // currentRoom has to be declared as a private static class member 
} 
相關問題