gpt4 book ai didi

java - 无法从静态上下文中引用非静态方法 (Java)

转载 作者:行者123 更新时间:2023-12-01 23:32:50 25 4
gpt4 key购买 nike

我是编程新手,并且在从 ant 类调用我的底层类时,不断发现无法从静态上下文中引用非静态方法。我已经删除了所有静态数据,但仍然收到此错误,如果有人可以指出我正确的方向或让我知道问题是什么,那就太好了,谢谢。

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];
}
}

最佳答案

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

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

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

关于java - 无法从静态上下文中引用非静态方法 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19084146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com