Can';我不能纠正我的java程序

Can';我不能纠正我的java程序,java,arrays,Java,Arrays,我刚开始学习java,正在编写一个程序。我在这里得到一个错误: locationsOfCells = simpleDotCom.getLocationCells(); 但我不确定错误是什么。日食说 无法对非静态方法进行静态引用 getLocationCells()来自类型SimpleTocom 有人能帮我吗?我做错了什么 public class simpleDotCom { int[] locationsCells; void setLocationCells(int[]

我刚开始学习java,正在编写一个程序。我在这里得到一个错误:

locationsOfCells = simpleDotCom.getLocationCells();
但我不确定错误是什么。日食说

无法对非静态方法进行静态引用
getLocationCells()
来自类型
SimpleTocom

有人能帮我吗?我做错了什么

public class simpleDotCom {
    int[] locationsCells;

    void setLocationCells(int[] loc){
        //Setting the array
        locationsCells = new int[3];
        locationsCells[0]= 3;
        locationsCells[1]= 4;
        locationsCells[2]= 5;
    }

    public int[] getLocationCells(){

        return locationsCells;

    }
}

public class simpleDotComGame {

    public static void main(String[] args) {
        printBoard();
    }

    private static void printBoard(){
        simpleDotCom theBoard = new simpleDotCom();
        int[] locationsOfCells; 
        locationsOfCells = new int[3];
        locationsOfCells = theBoard.getLocationCells();

        for(int i = 0; i<3; i++){
            System.out.println(locationsOfCells[i]);
        }

    }

}
公共类simpledtcom{
int[]位置cells;
void setLocationCells(int[]loc){
//设置数组
locationsCells=新的整数[3];
位置cells[0]=3;
位置cells[1]=4;
位置cells[2]=5;
}
public int[]getLocationCells(){
返回位置cells;
}
}
公共类simpledtomgame{
公共静态void main(字符串[]args){
印制板();
}
专用静态无效打印板(){
SimpleTocom theBoard=新SimpleTocom();
int[]细胞位置;
locationsOfCells=新整数[3];
locationsOfCells=theBoard.getLocationCells();

对于(inti=0;i您试图从静态上下文访问正常的非静态方法,它不起作用


您可以从尝试从中访问:
getLocationCells()
的例程中删除静态字,或者通过在声明中添加静态字使
getLocationCells()
成为静态字。

如果您试图从静态上下文访问正常的非静态方法,则无法工作


您可以从尝试从中访问的例程中删除静态字:
getLocationCells()
from,或使其成为
getLocationCells()
通过在声明中添加static单词来实现静态。

要么将SimpleTocom的字段和方法也设置为静态,要么创建SimpleTocom的实例并访问该实例的方法。

要么将SimpleTocom的字段和方法也设置为静态,要么创建SimpleTocom的实例并访问该实例的方法。

问题是调用
getLocationCells()
方法就像调用静态方法一样,而实际上它是一个实例方法

您需要首先从类中创建一个对象,如下所示:

simpleDotCom myObject = new simpleDotCom();
然后对其调用方法:

locationsOfCells  = myObject.getLocationCells();

顺便提一下,Java世界中有一种广泛采用的命名约定,类名总是以大写字母开头-您应该将类重命名为
simpledocom
,以避免混淆。

问题是您调用的是
getLocationCells()
方法,就好像它是一个静态方法,而实际上它是一个实例方法

您需要首先从类中创建一个对象,如下所示:

simpleDotCom myObject = new simpleDotCom();
然后对其调用方法:

locationsOfCells  = myObject.getLocationCells();

顺便说一句,Java世界中有一种广泛采用的命名约定,其中类名称总是以大写字母开头-您应该将类重命名为
SimpleTocom
,以避免混淆。

您正在尝试以静态方式
getLocationCells
。您需要创建
SimpleTocom
fir的实例st:

simpleDotCom mySimpleDotCom = new simpleDotCom();       
locationsOfCells = mySimpleDotCom.getLocationCells();
顺便说一句,类名总是以大写字母开头。这将有助于消除作为成员方法访问方法的混乱

更新:

要从更新的静态方法访问,还需要将
theBoard
声明为
static
变量:

static simpleDotCom theBoard = new simpleDotCom();

您正在以静态方式尝试
getLocationCells
。首先需要创建
SimpleTocom
的实例:

simpleDotCom mySimpleDotCom = new simpleDotCom();       
locationsOfCells = mySimpleDotCom.getLocationCells();
顺便说一句,类名总是以大写字母开头。这将有助于消除作为成员方法访问方法的混乱

更新:

要从更新的静态方法访问,还需要将
theBoard
声明为
static
变量:

static simpleDotCom theBoard = new simpleDotCom();

您试图从主方法引用非静态方法。这在java中是不允许的。您可以尝试将SimpleTocom类设置为静态,以便可以访问该类的方法。

您试图从主方法引用非静态方法。这在java中是不允许的。您可以尝试设置该simpleDotCom类是静态的,因此您可以访问该类的方法

simpleDotCom obj = new simpleDotCom();
locationsOfCells = obj.getLocationCells();
而且你的类名应该以大写字母开头


而且你的类名应该以大写字母开头,你的代码还有一些错误

  • 非静态方法无法使用类名调用。请尝试使用对象调用getLocationCells()

    SimpleTocom obj=新的SimpleTocom(); obj.getLocationCells()

  • 接下来,您将获得null指针异常。您尝试在初始化之前打印locationsOfCells值。因此,请在打印值之前尝试调用setLocationCells()方法

  • 你的方法定义void setLocationCells(int[]loc)。这里你有参数loc,但你没有使用方法块中的任何位置。因此请注意处理方法参数


  • 您的代码还有一些错误

  • 非静态方法无法使用类名调用。请尝试使用对象调用getLocationCells()

    SimpleTocom obj=新的SimpleTocom(); obj.getLocationCells()

  • 接下来,您将获得null指针异常。您尝试在初始化之前打印locationsOfCells值。因此,请在打印值之前尝试调用setLocationCells()方法

  • 你的方法定义void setLocationCells(int[]loc)。这里你有参数loc,但你没有使用方法块中的任何位置。因此请注意处理方法参数


  • 哦,谢谢你的帮助。我正在读的书还没有谈到静态。但是当我将静态添加到getLocationCells()时当我返回locationsCells时,它说给了我一个错误;
    Eclips给了我这个错误
    无法静态引用非静态字段locationsCells
    尝试将locationsCells设置为静态。谢谢你的帮助。我正在读的书