I am creating a sim game based on the 2nd level. I have a 2d array of gridSquares that many classes and methods access and change. Do I have to pass a gridSquares 2d array every time or make it global? What is the practice?
I thought it would be possible to create a class that simply contains a set of variables that could extend all classes? Is this a good or bad idea / not a good practice?
I'm still pretty new to java, so I'm still learning a lot!
Thanks in advance.
Rel
. Java - - . . 2D-; Board. , 2D-.
, :
public class Board { // This is what you're passing around now; Board hides it. // Square is the abstraction of a position on the Board private Square[][] grid; public Board(int nRows, int nCols) { this.grid = new Square[nRows][]; for (int i = 0; i < this.grid[i].length; ++i) { this.grid[i] = new Square[nCols]; } } // Now add methods for adding Pieces to the Board, rules for moving them, etc. }
-.
, , , .
, - "2d- gridSquares", singleton, Essentailly .
, , , , (, ).
, grid- (A) , A (import static A;). , A, .
import static A;
. , . . , .
, , , , GridSquaresAcessor. . , , , .
, GridSquaresAcessor .
, ...
, , 2D- .
Source: https://habr.com/ru/post/1707100/More articles:FCKEditor in asp.net-mvc - asp.net-mvcGoogle Web Toolkit: installing RichTextArea for a PHP application - javaIs everything all right if from one stored procedure I call another which sets lower transaction isolation level? - sql-serverHow can you just call Pitch () and Yaw () to force the camera to ultimately Roll ()? - c ++How to make a property public, but private, set in vb.net? - vb.netThe fastest way to find the minimum distance between points - algorithmWhat does the Warning: return do integer from a pointer without casting in C? - cBest Visual Studio 2008 Tutorial for Debugging? - debuggingWaitForMultipleObjects in Java - javaPHP text analysis and / or creating your own language? - phpAll Articles