加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oop – 聚合vs组合vs关联vs直接关联

发布时间:2020-12-14 01:07:14 所属栏目:百科 来源:网络整理
导读:我正在回顾我在面向对象编程中的知识。在类之间的关系主题,我遇到一些关系,这对我有点模糊。 我知道依赖“使用a”和继承“is-a”,但我有点不熟悉聚合,组合,关联和直接关联;也有,他们是“有-a”关系。一些使用聚合与协会可互换。 什么是直接关联?另外
我正在回顾我在面向对象编程中的知识。在类之间的关系主题,我遇到一些关系,这对我有点模糊。

我知道依赖“使用a”和继承“is-a”,但我有点不熟悉聚合,组合,关联和直接关联;也有,他们是“有-a”关系。一些使用聚合与协会可互换。

什么是直接关联?另外,什么是组合?在UML图中,表示它们的箭头是不同的。我会真的感谢,如果你能为我清除这些事情。

请注意,对“关联”定义有不同的解释。我下面的观点很大程度上取决于您在Oracle认证书和学习指南中阅读的内容。

临时关联

A usage inside a method,its signature or as a return value. It’s not really a reference to a specific object.

例如:我把车停在车库里。

组成关联

A so-called “STRONG relationship“: The instantiation of the linked object is often hard
coded inside the constructor of the object. It cannot be set from
outside the object. (Composition cannot be a many-to-many
relationship.)

示例:房子由石头组成。

直接关联

This is a “WEAK relationships“. The objects can live independent and there are usually setters or other ways to inject the dependent objects.

示例:汽车可以有乘客。

聚合关联

Very similar to a Direct association. It’s also a “WEAK relationship” with independent objects. However here the associated objects are a crucial part of the containing object.

示例:汽车应该有轮胎。

注意:直接关联和聚合关联通常被概括为“关联”。区别是微妙的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读