简体   繁体   中英

How Do I Create an object that holds more objects In Java?

Hi I'm doing a project in Java and I need to create a Diet object which will have 7 different days as attributes or perhaps make a separate class "DayOfTheWeek" so for example: Monday - Sunday, than each day must hold 6 objects of type Meal eg: meal1,meal2 and finally the Meal object must hold three objects of type Food so for example FoodItem1,FoodItem2 and its attributes.

The information for food will be retrieved from database, and the Diet object must be also stored into a database. My problem is that I don't know how to model this. My initial idea was to create Array of "Food" objects while creating a "Meal" object, and than while creating the "Day" object create an array of meals and so on until I create the Diet. However I would like some suggestions if there are better ways of doing it.

You could make an array in your Diet object that stores Day objects. Each day object has aggregates of Meal objects which has aggregates of Food. Essentially you would have an array in Diet object of Days, that have Meals, that has Food. You could have Food inherit from Meals as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM