Java,保存具有多个属性的数据的有效方法是什么?
Java, What would be an efficient way to save data with multiple attributes?
好的,所以我不太确定要问这个问题还是要为此搜索答案,所以我要举个例子。
假设我 运行 一家制造杯子的公司。每个不同的杯子都有不同的颜色,由不同的 material 制成,并且可以有不同的尺寸。假设我想编写一个程序来跟踪所有不同的杯子并保存它以便以后使用。例如:
大红杯 - Color:Red, Size:12oz, material:plastic
那么,在 java 中,如果我想允许用户创建尽可能多的项目并保存它们,那么执行此操作的有效方法是什么?
抱歉,我不是太有经验,如果这看起来含糊不清或者这是一个不好的问题,我很抱歉,但任何想法都将不胜感激!
你当然需要什么---(鼓声)一个class!
Java中的类是用来在一个地方存放一堆数据的。 (至少这是它的用途之一)您可以创建不同的 objects of a class.
您的 Cup
class 可能看起来像这样:
public class Cup {
public Color color;
public int size;
public Material material;
//Constructor
public Cup (Color color, int size, Material material) {
this.color = color;
this.size = size;
this.material = material;
}
}
Color
和 Material
是 枚举 。他们可能看起来像这样:
public enum Color {
Red, Green, Blue // Of course you an add other colors
}
public enum Material {
Wood, Glass, Plastic //Of course you an add others
}
你可以用这个制作一个大红杯:
Cup bigRedCup = new Cup (Color.Red, 15, Material.Plastic);
您可以通过以下方式参考杯子的属性:
bigRedCup.color
bigRedCup.size
bigRedCup.material
您还可以在 cup class 中添加 getter 和 setter 方法来添加一些封装。
关于 class 的更多信息:
https://docs.oracle.com/javase/tutorial/java/concepts/class.html
嗯,我想您要的是 data structure. An array would not be the optimal choice for your program cause you're saying you want to have the user as many as he wants and hence the data structure has to be dynamic, however arrays in Java have a fixed size, that has to be determined at compile time. I'd suggest you to take a look at the Java API implementation of a Linked List,它可以为您提供所需的所有可能性。它是动态的,您可以毫不费力地轻松添加、清除和删除内容。
如果您要搜索 "offline saving methods",我建议您将所有内容存储在数据库中,因此请查看 MySQL 和 JDBC driver mysql 在 Java 中。它为您提供了多种处理数据库的方法。
好的,所以我不太确定要问这个问题还是要为此搜索答案,所以我要举个例子。
假设我 运行 一家制造杯子的公司。每个不同的杯子都有不同的颜色,由不同的 material 制成,并且可以有不同的尺寸。假设我想编写一个程序来跟踪所有不同的杯子并保存它以便以后使用。例如:
大红杯 - Color:Red, Size:12oz, material:plastic
那么,在 java 中,如果我想允许用户创建尽可能多的项目并保存它们,那么执行此操作的有效方法是什么?
抱歉,我不是太有经验,如果这看起来含糊不清或者这是一个不好的问题,我很抱歉,但任何想法都将不胜感激!
你当然需要什么---(鼓声)一个class!
Java中的类是用来在一个地方存放一堆数据的。 (至少这是它的用途之一)您可以创建不同的 objects of a class.
您的 Cup
class 可能看起来像这样:
public class Cup {
public Color color;
public int size;
public Material material;
//Constructor
public Cup (Color color, int size, Material material) {
this.color = color;
this.size = size;
this.material = material;
}
}
Color
和 Material
是 枚举 。他们可能看起来像这样:
public enum Color {
Red, Green, Blue // Of course you an add other colors
}
public enum Material {
Wood, Glass, Plastic //Of course you an add others
}
你可以用这个制作一个大红杯:
Cup bigRedCup = new Cup (Color.Red, 15, Material.Plastic);
您可以通过以下方式参考杯子的属性:
bigRedCup.color
bigRedCup.size
bigRedCup.material
您还可以在 cup class 中添加 getter 和 setter 方法来添加一些封装。
关于 class 的更多信息:
https://docs.oracle.com/javase/tutorial/java/concepts/class.html
嗯,我想您要的是 data structure. An array would not be the optimal choice for your program cause you're saying you want to have the user as many as he wants and hence the data structure has to be dynamic, however arrays in Java have a fixed size, that has to be determined at compile time. I'd suggest you to take a look at the Java API implementation of a Linked List,它可以为您提供所需的所有可能性。它是动态的,您可以毫不费力地轻松添加、清除和删除内容。
如果您要搜索 "offline saving methods",我建议您将所有内容存储在数据库中,因此请查看 MySQL 和 JDBC driver mysql 在 Java 中。它为您提供了多种处理数据库的方法。