在 Java 中的另一个类中实例化/初始化的对象的引用变量 [英] Reference variable to an object instantiated/initialized in another class in Java

查看:18
本文介绍了在 Java 中的另一个类中实例化/初始化的对象的引用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之所以这么问是因为我收到了 NullPointerException.我现在这很容易,但我是一个很新的编程,发现这有点令人困惑.所以说我已经初始化了一个类中的一个对象,并想从另一个类访问同一个对象.

The reason I'm asking is because I'm getting NullPointerException. I now this is very easy but I'm pretty new programming and find this a bit confusing. So say I have initialized an object in a class and want to access that same object from another class.

比如现在我正在开发一个小型国际象棋游戏,在我的模型游戏类中,我有一个 Board 的实例,一个对象.反过来,Board 有一个 Squares 数组.正方形[][].

Like now for instance I'm working on a small Chess game, in my model Game class I have an instance of Board, an object. Board, in turn, has an array of Squares. Square[][].

游戏有棋盘,棋盘有 Square[][].

Game has board, board has Square[][].

现在,如果我想通过 Board 类型的对象板(在游戏中)访问 Square[][].我是只声明一个具有相同名称和类型的变量还是必须再次初始化它?

Now if I want to access the Square[][] through the object board (in Game) of type Board. Do I just declare a variable with the same name and type or do I have to initialize it again?

Board board OR Board board = new Board();

Board board OR Board board = new Board();

注意,我已经在类 Game 中初始化了 board,所以如果我再做一次,它们不会是两个完全不同的 Board 对象吗?

Note, I have already initialized board in the class Game so if I do it again, won't they be two totally different Board objects?

引用board"的类:

The class that refers to "board":

public class View extends JFrame {

Board      board;
JFrame     gameWindow   = new JFrame("Chess");
JPanel     gamePanel    = new JPanel();
JPanel[][] boardPanel   = new JPanel[8][8];
JMenuBar   gameMenu     = new JMenuBar();
JButton    newGame      = new JButton("New game");
JButton    pauseGame    = new JButton("Pause");
JButton    actionLog    = new JButton("Action log");

View(){
    gameWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
    gameWindow.setSize(400, 400);
    gameWindow.getContentPane().add(gamePanel);
    gameWindow.setVisible(true);
    gamePanel.setVisible(true);
    gameMenu.add(newGame);
    gameMenu.add(pauseGame);
    gameMenu.add(actionLog);
    for(JPanel[] row : boardPanel){
        for(JPanel box : row){
            gamePanel.add(box);
        }
    }
}

public void drawBoard(){
    for(int y = 0; y < 8; y++){
        for(int x = 0; x < 8; x++){
            Box box = new Box();
            box.setColour(board.getSquare(x, y).getColour());
            box.setCol(x);
            box.setRow(y);
            box.repaint();
            boardPanel[x][y].add(box);
        }
    }
}

}

class Box extends JComponent{
JPanel[][] boardPanel;
Board board;
Color boxColour;
int col, row;
public Box(){
    repaint();
}
public void paint(Graphics drawBox){
    drawBox.setColor(boxColour);
    drawBox.drawRect(50*col, 50*row, 50, 50);
    drawBox.fillRect(50*col, 50*row, 50, 50);
}
public void setColour(Color boxColour){
    this.boxColour = boxColour;
}

public void setCol(int col){
    this.col = col;
}

public void setRow(int row){
    this.row = row;
}

}

...以及实例化board"的类:

...and the class that instantiates "board":

public class Game {

@SuppressWarnings("unused")
public static void main(String[] args) 
        throws Throwable{
    Board board = new Board();
    View view = new View();
}

}

这里发生异常:

        for(JPanel[] row : boardPanel){
        for(JPanel box : row){
            gamePanel.add(box);
        }
    }

推荐答案

注意,我已经在类 Game 中初始化了 board,所以如果我再做一次,它们不会是两个完全不同的 Board 对象吗?

Note, I have already initialized board in the class Game so if I do it again, won't they be two totally different Board objects?

是的,您将拥有两个完全不同的实例.您已了解基本想法 - 您的程序中有对象实例,现在您必须让它们协同工作.

Yes, you'll then have two totally different instances. You're getting the basic idea - you have instances of objects in your program and now you have to get them to work together.

现在,如果我想通过 Board 类型的对象板(在游戏中)访问 Square[][].我是只声明一个具有相同名称和类型的变量还是必须再次初始化它?

Now if I want to access the Square[][] through the object board (in Game) of type Board. Do I just declare a variable with the same name and type or do I have to initialize it again?

您有 2 种方法可以让 Game 访问这些方块(根据您的看法,可能不止 2 种):

You have 2 ways to give Game access to the squares (probably more than just 2 depending on how you look at it):

1 让 Board 提供对 Squares 的访问(例如 Board 上返回 Squares 数组的 getter 方法),以便游戏可以访问它们.然后 Board 可以保存引用(有自己的实例变量来保存对方块的引用,或者每次都可以向 Board 请求引用).

1 Have the Board provide access to the Squares (e.g. a getter method on Board that returns the Squares array) so Game can access them. Board can then save the reference (have its own instance variable to hold the reference to squares, or can ask Board each time for the reference).

2 让棋盘提供方法来做游戏想要在格子上做的事情,即游戏要求棋盘对格子做某事,棋盘在格子上做动作.

2 Have the Board provide methods that do the things that Game wants to do on the the squares, i.e. the game asks the board to do something to the squares and the board does the action on the squares.

这篇关于在 Java 中的另一个类中实例化/初始化的对象的引用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆