所以这只是我代码的一部分,整个程序编译并运行,但我不断在以“GameBoard myBoard = this.getGameBoard();”开头的三行旁边看到“局部变量隐藏字段”。我'我只是好奇这实际上意味着什么,以及从长远来看它是否对我的程序有任何影响。
public void initialze(){
myBoard = getGameBoard();
obstacleLocations = myBoard.getObstaclePositions();
pastureLocations = myBoard.getPasturePositions();
GameBoard myBoard = this.getGameBoard();
ArrayList<GameLocation> obstacleLocations = myBoard.getObstaclePositions();
ArrayList<GameLocation> pastureLocations = myBoard.getPasturePositions();
GameLocation closestPasture = pastureLocations.get(0);
GameLocation closestObstacle = obstacleLocations.get(0);
这意味着你有两个同名的不同变量 -myBoard
。其中之一是您班级中的一个字段。另一种是局部变量,即在方法内声明的变量。
两个变量同名是个坏主意。它会使您的代码非常混乱且难以维护。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)