Java是否具有const引用等效项? [英] Does Java have a const reference equivalent?

查看:71
本文介绍了Java是否具有const引用等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一段代码:

  //游戏板由Square组成。玩家可以将GamePieces放置在正方形上。 
公共类CheckersBoard
{
public boolean PlaceGamePiece(GamePiece gamePiece,int nRow,int nColumn){
return m_theGameBoard [nRow] [nColumn] .PlaceGamePiece(gamePiece);
}

私人广场[] [] m_theGameBoard;
}

假设我正在测试PlaceGamePiece方法(使用junit),我需要访问m_theGameBoard,以便我可以查看它并验证GamePiece是否位于正确的Square上并具有正确的数据。



在C ++中,我要么进行测试给一个朋友分类,使其可以访问私有成员m_theGameBoard,或者我有一个函数返回一个不能修改的const GameBoard(因为它是const):

  const GameBoard& GetGameBoard()const {return m_theGameBoard; } 

现在我可以做我想在游戏板上做的所有检查了,但是我可以



Java不支持返回const引用或朋友类。所以我的问题是这样做的标准Java方法是什么?我是否只需要提供一堆get访问器,让我检查 Square 上的数据?



UPDATE :
我最终按照Kaleb Brasee的建议编写了一个GetPiece方法。

  public GamePiece GetGamePiece(Point pt){
返回新的GamePiece(m_theGameBoard [pt.GetRow()] [pt.GetColumn()]);
}

注意,我创建了一个新的GamePiece对象并返回该对象。我没有返回GameBoards内部参考,因此没有人可以修改游戏板,因为他们只有一个副本!真好!谢谢您的帮助,我们提供了一些非常好的建议。 / p>

解决方案

将变量定义为 protected ,这样,如果单元测试为



但是,我只添加一个公共方法 getPiece(int row,int col)返回那个正方形上的棋子(如果那里没有棋子,则返回null)。无论如何,您可能仍需要这样的方法,并且可以在测试中使用它。


Here's a snippet of code:

    //Game board is made up of Squares.  A player can place GamePieces on a Square.
    public class CheckersBoard
    {
        public boolean PlaceGamePiece(GamePiece gamePiece, int nRow, int nColumn) {
            return m_theGameBoard[nRow][nColumn].PlaceGamePiece(gamePiece);
        }

        private Square[][] m_theGameBoard;
    }

Let say I'm testing the PlaceGamePiece method (using junit) and I need to access the m_theGameBoard so I can look at it and verify the GamePiece was placed on the correct Square and has the correct data.

In C++ I'd either make the test class a friend so it can access the private member m_theGameBoard, or I'd have a function that returns a const GameBoard that cannot be modified (because it's const):

const GameBoard& GetGameBoard() const { return m_theGameBoard; }

Now I can do what ever checking I want to do on the game board, but I can't modify the game board because it's const.

Java doesn't support returning const references or friend classes. So my question is what is the standard Java way of doing this?? Do I have to just provide a bunch of get accessors that allow me check the data on the Square?

UPDATE: I ended up writing a GetPiece method as Kaleb Brasee suggested.

public GamePiece GetGamePiece(Point pt) {
    return new GamePiece(m_theGameBoard[pt.GetRow()][pt.GetColumn()]);
}

Notice I create a new GamePiece object and return that. I'm not returning the GameBoards internal reference, therefore no one can modify the gameboard because they only have a copy! Nice! Thanks for the help guys, some really good advice.

FYI: I keep changing the names of the objects when I post them on here, sorry if that confused anyone.

解决方案

Define the variable as protected, so that if the unit test is in the same package, it can access it.

However, I would just add a public method getPiece(int row, int col) that returns the piece on that square (or null if there's no piece there). It's likely you'll need a method like that anyway, and you could use it in your tests.

这篇关于Java是否具有const引用等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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