저는 프로그래밍에 익숙하지 않고 앤티 클래스에서 내 바닥 클래스를 호출 할 때 비 정적 메서드를 정적 컨텍스트에서 참조 할 수 없도록합니다. 누군가가 올바른 방향으로 나를 가리킬 수 또는 문제가 될 것이라고 알려주면 모든 정적을 제거하고 여전히이 오류가 발생했습니다. 감사합니다.정적 컨텍스트 (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