本文介绍了面向对象语言中的 HAS-A、IS-A 术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通读了这本书,里面有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:

  1. 房子建筑物(继承);
  2. 房子有一个房间(组成);
  3. 一所房子有一个居住者(集合).
  1. A House is a Building (inheritance);
  2. A House has a Room (composition);
  3. A House has an occupant (aggregation).

(2) 和 (3) 之间的区别很微妙,但要区分很重要.它们共同构成了关联的形式.有什么不同?组合意味着子对象不能脱离父对象的上下文(破坏房子,房间消失),而聚合意味着子对象可以独立存在(破坏房子,占用者去其他地方).

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 术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 16:19