在Java中,我有一個2維的對象數組,但我無法訪問對象的類方法中的任何對象數組。我該怎麼辦? 這裏是我的類:從對象類訪問2維數組。
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()方法是我試圖訪問數組的不同部分的地方,但失敗了。在這裏我有一些工作的虛擬代碼。
謝謝。
請遵循[Java命名約定](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html)並使用初始小寫方法名稱 – 2013-03-05 21:52:20