[英]What is the best way to to store data that the majority of classes in a program use?
To explain, i'm creating a simple game in Java using Swing. 解释一下,我正在使用Swing在Java中创建一个简单的游戏。 The size of the JFrame used is needed in a good deal of my classes.
我的很多类中都需要使用JFrame的大小。 At first i just made a super class to contain the variables, but i am forced to find something different because Java only lets you extend once, and i need to use the super class for different reasons.
起初,我只是制作了一个包含变量的超类,但由于Java只允许您扩展一次,所以我被迫寻找其他不同的东西,并且出于各种原因,我需要使用超类。 If that makes sense.
如果这样的话。
I'm storing multiple instances of the same information in memory. 我将同一信息的多个实例存储在内存中。
Variables such as: 变量如:
JFrame height and width in pixels and tiles JFrame的高度和宽度(以像素和图块为单位)
Map height and width in pixels and tiles 地图的高度和宽度(以像素和图块为单位)
"Tile" height and width “平铺”高度和宽度
Here's some code to paint the picture better: 这是一些可以更好地描绘图片的代码:
protected static int SCREEN_WIDTH = 928;//Pixels
protected static int SCREEN_HEIGHT = 672;//Pixels
protected static int TILE_WIDTH = 32;//Pixels
protected static int TILE_HEIGHT = 32;//Pixels
protected int MAP_WIDTH = 1280;//Pixels
protected int MAP_HEIGHT = 1280;//Pixels
protected int MAP_WIDTHtile = MAP_WIDTH / TILE_WIDTH;
protected int MAP_HEIGHTtile = MAP_HEIGHT / TILE_HEIGHT;
protected static int XTILES = SCREEN_WIDTH / TILE_WIDTH;//how many tiles can fit on display x axis;
protected static int YTILES = SCREEN_HEIGHT / TILE_HEIGHT;//how many tiles can fit on display y axis;
protected ArrayList<BufferedImage> images;
protected ArrayList<ArrayList<Integer>> displayArray2;
protected ArrayList<ArrayList<Integer>> mapGlobal2;
protected static int xIndex = 0;
protected static int yIndex = 0;
Almost all of those variables are needed multiple classes throughout my entire program. 在整个程序中,几乎所有这些变量都需要多个类。 And i'm declaring it multiple times as a result.
结果,我要多次声明。 I can't help but think i'm implementing this horribly and there has got to be a better way to do it.
我忍不住想我正在可怕地实现这一点,并且必须有一种更好的方法来做到这一点。 I also feel as though passing all this information through as parameters to class constructors isn't the best route.
我也觉得将所有这些信息作为参数传递给类构造函数并不是最佳途径。
Create a class just for those variables, instantiate it once and pass it around your code. 为这些变量创建一个类,实例化一次并将其传递给您的代码。
Make it static if there is only ever one instance. 如果只有一个实例,请将其设为静态。
You might even start adding methods so as to manipulate the data better.. ;-) 您甚至可能开始添加方法,以便更好地操作数据。.;-)
We take the JFrame example. 我们以JFrame为例。 For example: you need to set height of JFrame in another class MyClass.
例如:您需要在另一个类MyClass中设置JFrame的高度。 You may pass the JFrame instance variable in constructor of MyClass class.
您可以在MyClass类的构造函数中传递JFrame实例变量。 This is the constructor of Myclass.
这是Myclass的构造函数。
public MyClass(JFrame frame){ this.frame = frame;}
when you instantiate MyClass, you pass your frame instance MyClass my = new MyClass(frame)
实例化MyClass时,您传递框架实例
MyClass my = new MyClass(frame)
Now you have a handle of frame in your MyClass. 现在,您的MyClass中具有框架句柄。 Which you can use to call methods of frame any where such as
fame.setHeight(450);
您可以使用它在任意位置调用框架方法,例如
fame.setHeight(450);
This approach is more logical and Object Oriented, as you are calling frame methods by using frame instance. 这种方法更具逻辑性和面向对象,因为您正在使用框架实例来调用框架方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.