在 Java 中,我有一个二维对象数组,但我无法在对象的类方法中访问这些对象数组中的任何一个。我应该怎么办?这是我的课:
class GoPiece
{
final int boardSize = 19;
final int empty = 0;
final int black = 1;
final int white = 2;
int pieceType = empty;
int leftRight;
int downUp;
int turnPlayed;
boolean legal;
// GoPiece's Constructor with 3 parameters.
GoPiece(int blackOrWhite, int horizontalCoordinate, int verticalCoordinate)
{
pieceType = blackOrWhite;
leftRight = horizontalCoordinate;
downUp = verticalCoordinate;
if ((true));
}
// GoPiece's Constructor with 2 parameters.
GoPiece(int horizontalCoordinate, int verticalCoordinate)
{
pieceType = empty;
leftRight = horizontalCoordinate;
downUp = verticalCoordinate;
}
// GoPiece's Constructor with no parameters.
GoPiece()
{
leftRight = 0;
downUp = 0;
}
// Initialize an empty Go board full of GoPieces.
GoPiece[][] InitializeBoard()
{
GoPiece[][] intersection = new GoPiece[boardSize][boardSize];
for(int horizontal = 0; horizontal < boardSize; horizontal++)
{
for(int vertical = 0; vertical < boardSize; vertical++)
{
intersection[horizontal][vertical] = new GoPiece(horizontal,vertical);
}
}
return intersection;
}
// Make a piece a certain type: empty, black, or white.
public void SetType(int newType)
{
pieceType = newType;
}
public int GetType()
{
return pieceType;
}
public void CheckKill()
{
int foobar = this.GetType();
}
}
然后,我可以在程序的另一部分中使用 InitializeBoard() 来创建 GoPieces 的二维数组......这可行,但是除了我在 GoPiece 类的成员函数中引用的部分之外,我如何访问所有这些部分? 我尝试将整个数组传递给 GoPieces 函数之一,但这似乎不起作用。
围棋是中国古代棋盘游戏。上面的 CheckKill() 方法是我尝试访问数组的不同部分但失败的地方。在这里,我有一些工作虚拟代码。
谢谢你。