【问题标题】:Non-static method cannot be referenced from a static context (Java) [duplicate]不能从静态上下文(Java)中引用非静态方法[重复]
【发布时间】:2013-09-29 22:41:40
【问题描述】:

我是编程新手,并且在从我的 ant 类调用我的 floor 类时,无法从静态上下文中引用非静态方法。我已经删除了所有静态信息,但仍然收到此错误,如果有人能指出我正确的方向或让我知道问题所在,那就太好了,谢谢。

public class Ant {

    public final int RED = 0, BLUE = 1, NORTH = 0, 
            EAST = 1, SOUTH = 2, WEST = 3;
    public int color;

    public Ant(int size, int dir) {  
        size = size;
        dir = startDir;
        floor floor = new floor(size);
    }

    public int getRow() {
       return row; 
    }

    public int getCol() {
        return col;
    }

    public void makeMoves(int numMoves, int dir) {
        final int[][] offSet = {/* NORTH  */ {-1, 0},
                               /* EAST   */  {0, 1},
                               /* SOUTH  */  {1, 0},
                               /* WEST   */  {0,-1}};

        final int[][] newDir = {/* NORTH  */ {WEST, EAST},
                               /* EAST   */  {NORTH, SOUTH},
                               /* SOUTH  */  {EAST, WEST},
                               /* WEST   */  {SOUTH, NORTH}};
        //set start row, col, and direction
        row = col = size/2;

        for(int move = 1; move <= numMoves; move ++) {
            //make a move based on direction
            row = row + offSet[dir][0];
            col = col + offSet[dir][1];

            //turn based on color of new tile and direction
            color = floor.getTileColor(row, col);
            dir = newDir[dir][color];

            //change color of current tile
            floor.changeTileColor(row, col);
        }      
    }//End of makeMoves
}//End Ant class

public class floor {    
    int [][] grid;

    public floor(int size) {
        grid = new int[size][size];
    }

    public int getTileColor(int row, int col) {
        return grid[row][col];
    }

    public void changeTileColor(int row, int col) {
        int color = grid[row][col];
    }
}

【问题讨论】:

  • 哪一行给出了错误?
  • 请显示导致错误的代码行。
  • @hexafraction:对我来说似乎不是这样。他似乎在问,因为他认为自己没有任何静态代码,而不是因为他想知道为什么他不能从静态代码中调用非静态代码。
  • @Dolda2000 floor.changeTileColor(row, col);对我来说似乎足够静态。 CV 恕我直言。
  • @hexafraction:我不是说他是对的,我只是说这是他的意图。 :)

标签: java static non-static


【解决方案1】:

该代码由于其他原因无法编译。例如,在此构造函数中,未定义 startDir。虽然定义了大小,但这并没有做任何事情。它将参数大小分配给自身。你真的想要 this.size = size;

public Ant(int size, int dir)
    {  
        size = size;
        dir = startDir;

此外,row 和 col 没有在任何地方定义。如果您遇到有关静态的错误,我想知道您是否正在编译旧代码。

【讨论】:

    【解决方案2】:

    public static void main() 是一个静态上下文。它只有一个,而从技术上讲,每个Ant 对象都有一个makeMoves()。在Ant 的实例上调用它们,或者也将它们设为static。这就是要点,只需查找关键字 static 和/或上下文、范围即可了解更多信息。

    【讨论】:

      【解决方案3】:

      正如其他人所指出的,您正在编译的代码似乎与您发布的代码不匹配,因为发布的代码包含其他错误,而不仅仅是静态访问。

      但是,我认为您的基本问题(至少因为它认为提出的问题)是您认为您已经在 Ant 类中为地板定义了一个实例变量,而实际上您没有。您刚刚在Ant 构造函数中为其定义了一个局部变量,该变量在构造函数返回后立即被丢弃。

      然后,由于您在 floor 类本身和您可能认为是保持 Ant 楼层的变量之间存在命名冲突,因此您尝试调用 floor.changeTileColor,认为它会在Ant 的 floor 实例,但它编译时就好像它是对静态方法的引用一样。这是因为floor 在这里指的是类floor 本身,而不是指向它的实例的变量。

      要解决这个问题,请在 Ant 类中创建一个 floor 实例变量,而不仅仅是在构造函数中(不过,建议使用另一个名称,以避免更多的命名冲突)。

      【讨论】:

      • 正确答案!此外,OP 应该将类的名称从 floor 更改为 Floor 以符合 Java 命名标准。
      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2013-04-05
      相关资源
      最近更新 更多