2017-02-09 84 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; 
} 

希望这是有用的

相关问题