|
Revision 4601, 1.3 KB
(checked in by pmoura, 7 weeks ago)
|
|
Added svn:mime-type property to source files (set to text/x-logtalk).
|
-
Property svn:mime-type set to
text/x-logtalk
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 | /* |
|---|
| 2 | These objects illustrate a variant of the "diamond problem" using |
|---|
| 3 | a prototype hierarchy. |
|---|
| 4 | |
|---|
| 5 | In this simple case, a solution for making the overridden definition inherited |
|---|
| 6 | by the bottom object the visible one is implemented using the alias/3 predicate |
|---|
| 7 | directive. |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | % root object, declaring and defining a predicate m/0: |
|---|
| 12 | |
|---|
| 13 | :- object(a2). |
|---|
| 14 | |
|---|
| 15 | :- public(m/0). |
|---|
| 16 | |
|---|
| 17 | m :- |
|---|
| 18 | this(This), |
|---|
| 19 | write('Default definition of method m/0 in object '), |
|---|
| 20 | write(This), nl. |
|---|
| 21 | |
|---|
| 22 | :- end_object. |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | % an object descending from the root object, which redefines predicate m/0: |
|---|
| 26 | |
|---|
| 27 | :- object(b2, |
|---|
| 28 | extends(a2)). |
|---|
| 29 | |
|---|
| 30 | m :- |
|---|
| 31 | this(This), |
|---|
| 32 | write('Redefinition of method m/0 in object '), |
|---|
| 33 | write(This), nl. |
|---|
| 34 | |
|---|
| 35 | :- end_object. |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | % another object descending from the root object, which also redefines predicate m/0: |
|---|
| 39 | |
|---|
| 40 | :- object(c2, |
|---|
| 41 | extends(a2)). |
|---|
| 42 | |
|---|
| 43 | m :- |
|---|
| 44 | this(This), |
|---|
| 45 | write('Redefinition of method m/0 in object '), |
|---|
| 46 | write(This), nl. |
|---|
| 47 | |
|---|
| 48 | :- end_object. |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | % bottom object, descending from the two previous objects and, as such, inheriting |
|---|
| 52 | % two definitions for the predicate m/0; the overridden definition inherited from |
|---|
| 53 | % object "c2" is renamed using the alias/3 directive and then we redefine the |
|---|
| 54 | % predicate m/0 to call the renamed definition: |
|---|
| 55 | |
|---|
| 56 | :- object(d2, |
|---|
| 57 | extends(b2, c2)). |
|---|
| 58 | |
|---|
| 59 | :- alias(c2, m/0, c2_m/0). |
|---|
| 60 | |
|---|
| 61 | m :- |
|---|
| 62 | ::c2_m. |
|---|
| 63 | |
|---|
| 64 | :- end_object. |
|---|