[英]How to use variable from class inside method in java
So I have to write a minesweeper game for an assignment. 因此,我必须为任务编写扫雷游戏。 If I made a class for the Board, containing two 2D-arrays, one for the board value and one holding whether the user had already clicked there or not clicked. 如果我为Board创建了一个类,其中包含两个2D数组,一个为board值,另一个为用户是否单击过该数组。 I wrote the methods with the arguments of the two 2D-arrays. 我用两个2D数组的参数编写了方法。 How would I call those arrays in my main class? 我如何在主类中调用这些数组?
public class Board {
int x;
int y;
public char[][] board;
public char[][] reveal;
Board(int x, int y){
board = new char[x][y];
reveal = new boolean[x][y];
}
}
public class Mine{
public static void main(String[] args){
Board gameboard;
gameboard = new Board(5, 5);
???
Board.printBoard(board, reveal);
}
}
public void printBoard(char[][] board, boolean[][] test){
for(int i=0; i<=board.length; i+=1){
for(int j=0; j<board[i].length; j+=1){
if (test[i][j]==true){
System.out.print(board[i][j]);
}
else {
System.out.print('?');
}
}
System.out.println();
}
}
How would I call those arrays in my main class? 我如何在主类中调用这些数组?
You don't 'call' an array. 您不会“调用”数组。 You call methods. 您调用方法。 If you want to access an instance variable of a class, you need to provide accessor methods, ie methods that 'get' and 'set' the instance variable. 如果要访问类的实例变量,则需要提供访问器方法,即“获取”和“设置”实例变量的方法。
Since you are declaring board
and reveal
attributes as public you can just access their values using gameboard.board
and gameboard.reveal
. 由于您要声明游戏board
并公开reveal
属性,因此您可以使用gameboard.board
和gameboard.reveal
访问它们的值。
However, this is a very very bad approach. 但是,这是一个非常非常糟糕的方法。 Encapsulation . 封装 。
So you should declare getter and setter for each class attribute x
and access its value with: objectName.getX()
因此,您应该为每个类属性x
声明getter和setter,并使用以下方法访问其值: objectName.getX()
After you instantiate an instance of your board class in your main method, you would access them as follows (for example): 在main方法中实例化Board类的实例后,可以按如下方式访问它们:
Board gameboard = new Board(5,5);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(gameboard.board[i][j]);
}
}
etc. 等等
Using private members and accessor methods is a better a approach but certainly not a requirement. 使用私有成员和访问者方法是一种更好的方法,但当然不是必需的。
您只需键入gameboard.board即可获取存储在Board对象中的“ board”。
You can access method easily as other people have suggested by using the following code. 您可以使用下面的代码轻松地访问其他人建议的方法。
System.out.print(gameboard.board[i][j]);
You could incorporate encapsulation like many have suggested. 您可以像许多建议的那样合并封装。
However, I'd also suggest implementing it simpler, and within one class if you're allowed to for your assignment. 但是,如果允许您进行作业,我还建议更简单地实现它,并且在一个类中实现。
This works just as effectively with less code and IMO is just as easy to understand. 只需更少的代码,这同样有效,并且IMO也易于理解。
public class Board {
private int x;
private int y;
private char[][] board;
private boolean[][] reveal;
public static void main(String[] args) {
Board gameboard = new Board(5, 5);
gameboard.printBoard();
}
public Board(int x, int y) {
this.x = x;
this.y = y;
board = new char[x][y];
reveal = new boolean[x][y];
}
public void printBoard() {
for (int i = 0; i < board.length; i += 1) {
for (int j = 0; j < board[i].length; j += 1) {
if (reveal[i][j] == true) {
System.out.print(board[i][j]);
} else {
System.out.print('?');
}
}
System.out.println();
}
}
public class Mine {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.