我应该使用什么数据结构来存储不同类型的对象
我想为书店构建一个简单的应用程序。出售的商品有几种:书籍、电影和杂志。我正在考虑构建一个带有 toString()
方法的抽象类,以及 3 个子类 - 书籍、电影、杂志(稍后还会有更多)。然后在程序中我想对这些对象进行操作,并想将它们全部保存在 ArrayList
中。这是一个好的数据结构选择吗?
I want to build a simple application for a bookstore. There are a few types of things on sale: books, movies, and magazines. I was thinking to of building one abstract class with a toString()
method, and 3 subclasses - books, movies, magazines (there will be more later). Then in the program I want to operate on these objects and was thinking I would keep them all in an ArrayList<ParentClass>
. Is this a good choice of data structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,我建议您使用泛型。例如,如果您要定义抽象类(或者可能更好的接口
Item
),则创建new ArrayList
,这样您将无法在其中添加任何内容,除了项目
。二、使用接口。您的定义应如下所示:
List lll = new ArrayList- ();
主要数据结构的选择取决于您的需要。我相信您将执行某种搜索机制。在这种情况下,您可能可以使用
Map
或组合列表(按顺序存储所有对象)和多个映射(帮助您通过不同参数执行搜索)。无论如何,您应该通过某个类(模型)来包装您的数据结构,该类(模型)提供业务逻辑的基本功能,因此,如果您想更改某些内容,您可以在此类中完成,而无需对应用程序的其他部分进行任何更改。
First, I'd recommend you to use generics. If for example you are going to define abstract class (or probably better interface
Item
) so createnew ArrayList<Item>
, so you will not be able to add there anything exceptItem
.Second, use interface. You definition should look like:
List<? extends Item> lll = new ArrayList<Item>();
The choice of the main data structure depends on your needs. I believe that you are going to perform some kind of search mechanisms. In this case probably you can use
Map
or combine list where you store all objects sequentially and several maps that help you to perform search by different parameters.Anyway you should wrap your data structure by some class (model) that provides basic functionality of your business logic, so if you want to change something you can do it within this class without any changes in other parts of your application.
数据结构很大程度上取决于您需要对数据执行的操作。如果您要做的只是循环调用 toString 方法的所有对象,那么就这样做吧。如果您需要对 ArrayList 无法满足的数据执行某些操作,那么最好开始考虑其他选项。
The data structure is quite dependent on what you need to do with the data. If all you're going to do is loop over all the objects calling your toString method, then go for it. If you need to do something with your data for which an ArrayList becomes deficient, then it's best to start looking at your other options.
您可以创建诸如
BookstoreEntity
抽象超类(或接口)之类的东西,其中具有您要操作的项目的共同特征。如颜色
、材质
、价格
等...您可以在抽象超类中定义 setter 和 getters 方法。
BookstoreEntity
对象列表将包含IS-A BookstoreEntity
的任何内容。您可以在 for 循环中操作通用方法(在超类中定义)(例如
getColor
、getPrice
等...)。例如,
You can create something like
BookstoreEntity
abstract super class (or interface) with the common characteristics of the items you want to operate on. Likecolor
,material
,price
etc...You can define the setters and getters method in the abstract super class. A list of
BookstoreEntity
objects will hold anything thatIS-A BookstoreEntity
.You can operate on the common methods (defined in the super class) in a for loop for example (like
getColor
,getPrice
etc...)Regards!
显然,您的问题不是元素的访问方法。因此任何容器类型(列表、地图、矢量等)都可能适合您的访问需求。
我想,更清晰和全面的问题陈述是,是否在面向对象范式的上下文中发出对对象的一般处理。
然而,任何类的对象都可以放在任何通用容器中,而无需任何抽象(通过继承、接口或抽象类),因为默认情况下所有类都扩展了 Object 类。如果不需要任何一般处理,例如对每个元素进行相同的操作,如问题标题所示,也可以使用:
ArrayList
但是,大多数人发现这种方法毫无用处,因为当将这些对象放在一起以相同的方式使用时,它没有任何意义。我想,你应该弄清楚这些对象的一般处理方式,然后声明一些属性。之后创建一个父类并用它定义容器。
Apparently , your problem is not the access method of an element. So any container type (List, Map, Vector etc.) may suit your access needs.
More clear and comprehensive statement of problem , I guess , is that if a general treatment of objects is issued in context of object-oriented paradigm.
However, objects of any class can be put together in any generic container without any abstraction (via inheritance, interfaces or abstract classes) since all classes extends Object class by default. If any general treatment , like operating same operation on each element , is not required, as the title of question suggests , this can used as well :
ArrayList<Object> bookStore
.But , most of people find this method useless since it doesn't make any sense when regarding these objects are put together to be used in same way. I guess , you should work out to determine the ways of general treatments of these objects and then declare some properties. After that create a parent class and define the container with it.
如果您想采用抽象类方式,我建议设置公共属性。例如,当您使用书店的示例时,我确信所有东西都有价格。您可能还可以将其他一些事物与它联系起来。这将使您免于在子类中一遍又一遍地重新实现简单的事情。
与从抽象类继承相比,我更喜欢接口。我建议定义
BookstoreEntity
接口而不是使用抽象类。我还建议您创建一个类
BookStore
或ProductManager
或类似的东西来定义您的程序作为一个整体执行的操作。它可能包含一个可以很好地表示它的本机数据结构(可能是Map
,其中 int 是产品 id)。 我建议您在这种情况下不要对数据结构进行子类化。If you want to go the abstract class way, I'd recommend setting common properties. For example, as you are using an example of a bookstore, I'm sure that everything has a price. There are probably other things you could associate with it as well. This will save you from reimplementing simple things over-and-over again in your subclasses.
I'm a bigger fan of Interfaces than I am of inheriting from abstract classes. I'd suggest defining a
BookstoreEntity
interface instead of using an abstract class.I'd also recommend that you make a class
BookStore
orProductManager
or something of the like that defines things your program does as a whole. It may contain a native datastructure that represents it nicely (maybeMap<int, BookStoreEntity>
where int is the product id). I recommend that you do not subclass the datastructure in this case.