我更新的編程,並繼續獲取非靜態方法不能從靜態上下文從我的螞蟻類調用我的地板類時引用。我刪除了所有的靜態信息,並且仍然出現這個錯誤,如果有人能指出我的方向是正確的,還是讓我知道這個問題會很好,謝謝。非靜態方法不能從靜態上下文中引用(Java)
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];
}
}
哪一行給出錯誤? – ssantos
請顯示導致錯誤的代碼行。 –
@hexafraction:這對我來說似乎不是這樣。他似乎在問,因爲他不認爲他有任何靜態代碼,並不是因爲他想知道他爲什麼不能從靜態代碼中調用非靜態代碼。 – Dolda2000