2017-02-09 79 views
-2

當我嘗試訪問另一個類的方法時,它給了我一個錯誤,即無法從靜態方法訪問非靜態方法,但我的方法都不是靜態方法。我無法訪問另一個類的處理器方法? java

import java.util.ArrayList; 
    public class creatureClassDriverRathtarsGame 
    { 
public static void main(String[] args) 
{ 
    creatureClass player = new creatureClass("name", 14,new locationClass(0, 0, 0)); 
    ArrayList <locationClass> locationRathTars = new <locationClass> ArrayList(5); 
    for(locationClass r: locationRathTars) 
    { 
     int randomRow = (Math.random() * ((locationClass.getMaxRow()) + 1)); 
     int randomCol = (Math.random() * ((locationClass.getMaxCol()) + 1)); 
     creatureClass rathtars = new creatureClass("rathtars",0, new locationClass(randomRow, randomCol, 0)); 
    } 

和被稱爲acessor方法是

public int getMaxRow() 
{ 
    return maxrow; 
} 
public int getMaxCol() 
{ 
    return maxcol; 
} 
+3

請張貼實際[MCVE。現在,我假設你的類被稱爲'locationClass',你實際上正在靜態地調用它的方法。 – CollinD

+2

遵循基本的Java代碼慣例會指出問題。類名必須以大寫字母開頭。一個類的實例(對象)應該有一個駱駝式的名字。 – Slava

+2

當您想要調用r.getMaxRow()時,您正在調用locationClass.getMaxRow() – antlersoft

回答

0

首先,你必須爲Java類和對象的基礎知識和基本的靜態和非靜態成員

之間的區別要使用類名稱調用方法,它必須是靜態的

所以你的處理器方法應該看起來像自爆

public static int getMaxRow() 
{ 
    return maxrow; 
} 
public static int getMaxCol() 
{ 
    return maxcol; 
} 

希望這是有用的

相關問題