简体   繁体   English

存储程序中大多数类使用的数据的最佳方法是什么?

[英]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.

相关问题 将操作存储为java中的数据的最佳方法是什么? - What is the best way to store actions as data in java? 在java中的程序运行之间存储数据的最佳方法? - Best way to store data between program runs in java? 在不同的JUnit测试类之间共享数据的最佳方法是什么 - What is the best way to share data between different JUnit test classes 什么是从数据库存储和检索数据的最佳方法 - What is the best way to store and retrieve data from database 只要Tomcat工作,什么是存储数据的最佳方法? - What is the best way to store Data as long as Tomcat works 从 Excel 读取数据后存储数据的最佳方式是什么? - What is the best way to store data after reading from Excel? 无法使用数据库时存储数据的最佳方法是什么? - What is the best way to store data when a database could not be used? 在android中存储JSON数据对象的最佳方法是什么? - What is the best way to store a JSON data object in android? 在 Java 中存储一些数据的最佳方法是什么? (数组与数组列表) - What is the Best Way to Store some data in Java? (Array vs ArrayList) 为 android 应用程序存储 static 数据的最佳方式是什么? - What is the best way to store static data for an android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM