我应该使用全局变量还是在java中传递变量?

发布于 2024-07-17 12:58:28 字数 260 浏览 2 评论 0原文

我正在创建一个基于 2d 图块的模拟游戏。 我有一个 gridSquares 的二维数组,可以从许多不同的类和方法访问和更改它们。 我应该每次传递 gridSquares 的二维数组,还是将其设为全局数组? 哪个是最佳实践?

我在想,是否可以选择创建一个只包含一组所有类都可以扩展的变量的类? 这是一个好主意还是坏主意/不是一个好的做法?

我对java还很陌生,所以我还在学习很多东西!

提前致谢。

相对值

I'm creating a 2d tile based sim game.
I have a 2d array of gridSquares, which are accessed and changed from many different classes and methods.
Should I pass the 2d array of gridSquares each time, or make it a global? Which is best practice?

I was thinking, would it be an option to create a class which just contains a set of variables which all classes could extend? Is that a good or bad idea / not good practice?

I'm still fairly new to java so I'm still learning lots!

Thanks in advance.

Rel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌路黄昏 2024-07-24 12:58:28

您不应该根据数据结构进行设计。 Java 是一种面向对象的语言。 尝试将您的问题视为相互作用的对象。 它不是一个二维数组; 它是一个 Board 对象。 构建用于在问题中操纵其状态的行为,并隐藏您碰巧选择了二维数组的事实。

我还没有制定出董事会的所有细节,但它会这样开始:

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.
} 

You should not be designing in terms of a data structure. Java's an object-oriented language. Try thinking about your problem as objects interacting. It's not a 2D array; it's a Board object. Build the behavior for manipulating its state into the problem and hide the fact that you happen to have chosen a 2D array.

I don't have all the details of a Board worked out, but it would start like this:

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.
} 
我的痛♀有谁懂 2024-07-24 12:58:28

将它们传递到构造函数中并将它们保存在成员变量中。

如果您从太多地方访问它们,则可能会遇到设计问题。

Pass them in constructors and hold them in member variables.

If you access them from too many places you probably have a design problem.

东京女 2024-07-24 12:58:28

如果您确定只有一个“二维网格数组”,您可以始终使用单例模式,本质上使其成为全局的。

不过,对此存在支持和反对的争论,您可能会发现有一天您希望能够预加载地图,但单例会妨碍(无法创建单例的第二个实例)。

If your certain your only going to ever have one '2d array of gridSquares' you could always use the singleton pattern, essentailly making it global.

There are arguements for and against this though, you may find one day you want to be able to preload maps, but the singleton gets in the way (cant create a second instance of a singleton).

风情万种。 2024-07-24 12:58:28

如果确实需要,请在包含网格的类 (A) 中将网格声明为公共,然后静态导入 A 类 (import static A;)。 这将使您至少能够与网格交互而无需扩展 A 类。

您的代码不应该从太多地方访问网格。 考虑重构您的代码,以避免必须从各处操作网格。 分开你的担忧。 正如您提到的,使用继承绝对不是一个好主意。

If you absolutely need to, in your grid-containing class (A) declare the grid as public and then import the A class statically (import static A;). This will enable you to interact with the grid without extending the A class, at least.

Your code should not be accessing the grid from too many places. Consider re-factoring your code to avoid having to manipulate the grid from all over the place. Separate your concerns. And it's definitely not a good idea to use inheritance, like you mentioned.

水水月牙 2024-07-24 12:58:28

我建议将其设为静态,但创建一个明确的类来从中读取数据,例如 GridSquaresAcessor。 在此类中,您将编写访问数组的所有方法。 更好的是,将其设为此类的私有静态字段以避免任何其他代码
以此类中未定义的方式进行操作。

在每个需要访问数组的类中,您可以传递一个 GridSquaresAcessor 作为构造函数的参数,并将其存储在局部变量中。

我个人不喜欢单例,因为它们使测试代码变得非常困难......

如果您的代码是多线程的,请务必使 2D 数组同步。

What I would recommend it to make it static but create one definite class to read the data from it, say GridSquaresAcessor. In this class you write all the methods to access the array. Even better, make it a private static field of this class to avoid any other code
to manipulate in a way not defined in this class.

In every class you need access to the array you can pass one GridSquaresAcessor as a parameter on the constructor and keep it stored in a local variable.

I personally don't like singletons because they make it very hard to test code...

If your code is multithreaded, be sure to make the 2D-array synchronized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文