星期二, 7月 10, 2007

Inheritance - IS-A

在《Fundamentals of Data Structures in C++》中看到一段話


Inheritance is used to express subtype relationships between ADTs. This is also referred to as the IS-A relationship. We say that a data object of Type B IS-A data object of Type A if B is more specialized than A or A is more general than B; that is, all Bs are As, but not all As are Bs; or the set of all objects of Type B is a subset of the set of all objects of Type A. For example, Chair IS-A Furniture, Lion IS-A Mammal, Rectangle IS-A Polygon.

IS-A 及 HAS-A 是 OO 兩個很特殊的關係。HAS-A 就好比組裝,如:汽車有四個輪子,這代表每一個汽車物件其內部皆有四個輪子物件。簡單說就是物件中包有物件的關係。

而 IS-A 就有點像在探討本質,像:圓形窗子雖然改造過了,但它還是個窗子;桌子雖然是眾多家具之一,然而,他仍是種家具。

而 IS-A 的關係正解釋了為什麼 pointer to base class 可以指向 object of derived class 。

沒有留言: