【发布时间】:2023-03-20 02:00:02
【问题描述】:
我不太清楚这个问题该怎么说,所以就这样吧。在一节课中,我有这个:
public class Board {
//instance variables
private char BLANK = '_';
private char[][] board;
public Board(){
int SIZE = 9;
char[][] board = new char [3][3];
for (int i=0; i<board.length; i++){
for (int j=0; j<board[i].length; j++){
board[i][j] = BLANK;
}
}
}
}
在另一个('class'标签被省略):
public int getComputerSpot(){
int nbr = r.nextInt(Board.SIZE)+1;
while (!board.isAvailable(nbr)){
nbr = r.nextInt(Board.SIZE)+1;
}
return nbr;
}
找不到 Board.SIZE;它说它找不到变量。缺少什么,或者我该如何确保它可以找到 Board.SIZE?谢谢!
【问题讨论】:
-
这种方式只能访问类变量。 programmerinterview.com/index.php/c-cplusplus/…
-
SIZE是构造函数的局部变量。它在外面是不可见的。 -
实际上,您似乎对实例变量的概念很熟悉(来自您的
//instance variables评论)。这对您来说应该不足为奇。 -
@Ben 在您提出的 14 个问题中,您只接受了一个答案。请花时间投票/接受您的问题的答案,以感谢那些捐赠空闲时间来帮助您的人。
标签: java class object methods call