Author:halw

Date:2008-10-14T17:15:25.000000Z


git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@81 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
halw
2008-10-14 17:15:25 +00:00
parent 062493b5b9
commit 7bf0b00d90
6 changed files with 110 additions and 79 deletions

View File

@@ -7,14 +7,14 @@ Inheritance, the other fundamental generalization mechanism, makes it possible t
The following simple example, from the Data Structure Library in EiffelBase, is typical. <code> LIST </code>, as noted, describes lists in any representation. One such representation if the lists have a fixed number of elements uses an array. We may define the corresponding class by combination of <code> LIST </code> and <code> ARRAY </code>, as follows:
<code>
class ARRAYED_LIST [G]
inherit
LIST [G]
ARRAY [G]
inherit
LIST [G]
ARRAY [G]
export ... See below ... end
export ... See below ... end
feature
... Specific features of fixed-size lists ...
... Specific features of fixed-size lists ...
end -- ARRAYED_LIST
</code>
@@ -22,17 +22,21 @@ end -- ARRAYED_LIST
The <code> inherit </code> ... clause lists all the "parents" of the new class, which is said to be their "heir". (The "ancestors" of a class include the class itself, its parents, grandparents etc.; the reverse term is "descendant".) Declaring <code> ARRAYED_LIST </code> as shown ensures that all the features and properties of lists and arrays are applicable to arrayed lists as well. Since the class has more than one parent, this is a case of multiple inheritance.
Standard graphical conventions -- drawn from the Business Object Notation or BON, a graphical object-oriented notation based on concepts close to those of Eiffel, and directly supported by EiffelStudio -- illustrate such inheritance structures:
[[Image:invitation-4]]
[[Image:invitation-4]]
An heir class such as <code> ARRAYED_LIST </code> needs the ability to define its own export policy. By default, inherited features keep their export status (publicly available, secret, available to selected classes only); but this may be changed in the heir. Here, for example, <code> ARRAYED_LIST </code> will export only the exported features of <code> LIST </code>, making those of <code> ARRAY </code> unavailable directly to <code> ARRAYED_LIST </code> 's clients. The syntax to achieve this is straightforward:
<code>
class ARRAYED_LIST [G]
inherit
LIST [G]
ARRAY [G]
inherit
LIST [G]
ARRAY [G]
export {NONE} all end
export {NONE} all end
... The rest as above ...
... The rest as above ...
</code>
Another example of multiple inheritance comes from a windowing system based on a class <code> WINDOW </code>, close to actual classes in EiffelVision 2. Windows have '''graphical''' features: a height, a width, a position, routines to scale windows, move them, and other graphical operations. The system permits windows to be nested, so that a window also has '''hierarchical''' features: access to sub windows and the parent window, adding a sub window, deleting a sub window, attaching to another parent and so on. Rather than writing complex class that would contain specific implementations for all of these features, it is preferable to inherit all hierarchical features from <code> TREE </code> (a class in EiffelBase describing trees), and all graphical features from a class <code> RECTANGLE </code>.
@@ -42,18 +46,18 @@ Inheritance complements the "client" relation by providing another form of reuse
The very power of inheritance demands adequate means to keep it under control. Multiple inheritance, in particular, raises the question of name conflicts between features inherited from different parents; this case will inevitably arise in practice, especially for classes contributed by independent developers. You may remove such a name conflict through '''renaming''', as in
<code>
class C
inherit
A
rename
x as x1,
y as y1
end
inherit
A
rename
x as x1,
y as y1
end
B
rename
x as x2,
y as y2
end
B
rename
x as x2,
y as y2
end
feature ...
</code>