问题描述
我刚读完这本书,里面有"HAS-A"和"IS-A"这两个词.有人知道他们的具体含义吗?尝试在这本书中进行搜索,但该书长600页.
I was just reading through the book and it had the terms, "HAS-A" and "IS-A" in it. Anyone know what they mean specifically? Tried searching in the book, but the book is 600 pages long.
推荐答案
这是面向对象的编程和UML术语,不是特定于Java的.您实际上应注意以下三种情况:
This is object-oriented programming and UML terminology, not Java-specific. There are actually three cases you should be aware of:
- 房屋是建筑物(继承);
- 一所房子有一间房间(组成);
- 一所房子有一个居住者(集合).
- A House is a Building (inheritance);
- A House has a Room (composition);
- A House has an occupant (aggregation).
(2)和(3)之间的区别是微妙的,但重要的是要区分.它们在一起就是 association 的形式.有什么不同?组合意味着子对象不能生活在父对象的上下文之外(破坏房屋和房间消失),而聚集意味着子对象可以独自存在(破坏房子并且居住者去其他地方).
The difference between (2) and (3) is subtle yet important to differentiate. Together they are forms of association. What's the difference? Composition implies the child object cannot live out of the context of the parent (destroy the house and rooms disappear) whereas aggregation implies the child can exist on its own (destroy the house and the occupant goes elsewhere).
这篇关于面向对象语言中的HAS-A,IS-A术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!