【发布时间】:2018-05-04 03:44:26
【问题描述】:
【问题讨论】:
【问题讨论】:
你不能在类体中赋值,除非你把它写在初始化块或构造函数中。所以写在下面提到的块中或在构造函数中赋值。
public class Maze{
private int maze[][] = new int[5][5];
//Changing the value using initializer block
{
maze[1][1] = 1;
}
//Changing the value using constructor
public Maze(){
maze[1][1]=5;
}
public int[][] getMaze() {
return maze;
}
public void setMaze(int[][] maze) {
this.maze= maze;
}
public static void main(String args[]) {
Maze maze = new Maze();
int maze[][] = maze.getMaze();
//Changing the value after creating object
maze[1][2] = 5;
}
}
【讨论】:
分配值的一种简单方法是使用 Maze 类编写自定义方法,并在您的 main 方法中使用它。例如:
private void updateMaze(int val, int i, int j) {
maze[i][j] = val;
}
根据用例,可以使用不同的访问修饰符。
【讨论】: