问题
I'm having trouble grasping the concept of composition.
I need to create a manufacturer class and a products class and use composition.
Do I make the has-a reference inside products and just add the manufacturer object when creating a new product?
回答1:
That is exactly what is intended, and it makes sense. The manufacturer is a perfectly reasonable property of a product, and it seems reasonable to have a reference to the manufacturer in the object.
About your list, you're not being asked to have a list of products given a manufacturer (at least as far as I can tell). So you don't need the link to go in that direction, and thus not the list you appear to be mentioning.
来源:https://stackoverflow.com/questions/39947396/java-composition-has-a-relationship-clarification