Java 我无法访问另一个类的acessor方法?爪哇
当我试图访问另一个类的方法时,它给了我一个错误,即不能从静态方法访问非静态方法,但我的方法都不是静态的Java 我无法访问另一个类的acessor方法?爪哇,java,Java,当我试图访问另一个类的方法时,它给了我一个错误,即不能从静态方法访问非静态方法,但我的方法都不是静态的 import java.util.ArrayList; public class creatureClassDriverRathtarsGame { public static void main(String[] args) { creatureClass player = new creatureClass("name", 14,new locationClas
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));
}
首先,您必须具备java类和对象的基本知识,以及静态和非静态成员之间的区别 要使用类名调用方法,它必须是静态的 所以你的acessor方法应该看起来像
public static int getMaxRow()
{
return maxrow;
}
public static int getMaxCol()
{
return maxcol;
}
希望这是有用的请发布一个实际的。目前,我假设您的类被称为
locationClass
,实际上您是在静态地从中调用方法。类名必须以大写字母开头。类的实例(对象)应具有以驼峰大小写的名称。要调用r.getMaxRow()时,您正在调用locationClass.getMaxRow()
public static int getMaxRow()
{
return maxrow;
}
public static int getMaxCol()
{
return maxcol;
}