Java组成(has-a)关系澄清 [英] Java composition (has-a) relationship clarification

查看:157
本文介绍了Java组成(has-a)关系澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解组成概念时遇到麻烦。

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?

推荐答案

这正是预期的目的,并且很有意义。制造商是产品的完全合理的属性,在对象中引用制造商似乎是合理的。

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.

这篇关于Java组成(has-a)关系澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆