Java composition (has-a) relationship clarification

為{幸葍}努か 提交于 2019-12-10 18:49:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!