简体   繁体   English

如何在Java中使用类内部方法中的变量

[英]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.boardgameboard.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.

相关问题 如何通过Java使用2类中1类的方法变量? - How to use variable of method from 1 class in 2 class via Java? 如何使用方法内部定义的变量在Java中同一类的另一个方法中的方法外部使用 - How to use a variable defined inside a method use outside the method in another method on the same Class in java Java - 如何在导入主类时使用方法中的变量 - Java - How to use a variable from a method when imported into the main class 如何在另一个 class 中的一个类/方法中使用变量? - How to use a variable inside one class/method inside another class? 如何使用类运行器中的变量在另一个类的方法中使用? (BlueJ,Java) - How can I use a Variable from a Class Runner to use in a Method in another Class? (BlueJ, Java) 如何在另一个类中使用方法中的变量? - How to use a variable from method in another class? 如何在另一个类中的方法(在Try-Catch内部)中使用变量? - How to use a variable from a method (inside Try-Catch) in another class? JAVA-尝试从类外部执行的操作中使用变量 - JAVA - Trying to use a variable from inside an actionperformed outside the class 我如何在Java中的外部类的活动中使用方法 - How can I use a method inside an activity from external class in java 如何从动作侦听器/类构造函数访问变量以在java的main方法中使用 - How to access variable from action listener/class constructor to use in main method in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM