75392e54c7
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@757 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
106 lines
1.8 KiB
Plaintext
106 lines
1.8 KiB
Plaintext
=================================================================
|
|
Logtalk - Object oriented extension to Prolog
|
|
Release 2.15.0
|
|
|
|
Copyright (c) 1998-2003 Paulo Moura. All Rights Reserved.
|
|
=================================================================
|
|
|
|
|
|
|
|
% we can start by asking joe its age:
|
|
|
|
| ?- joePerson::age(Age).
|
|
|
|
Age = 30
|
|
yes
|
|
|
|
|
|
% the same question could be made via any of its viewpoints:
|
|
|
|
| ?- joeSportsman::age(Age).
|
|
|
|
Age = 30
|
|
yes
|
|
|
|
|
|
% now let's tell joe to get older:
|
|
|
|
| ?- joePerson::getOlder.
|
|
|
|
yes
|
|
|
|
|
|
% we can verify the effect of the above message from any of the viewpoints:
|
|
|
|
| ?- joeChessPlayer::age(Age).
|
|
|
|
Age = 31
|
|
yes
|
|
|
|
|
|
% because the growOld/0 and the age/1 predicates are implemented using
|
|
% property sharing, we can send the getOlder/0 message to any viewpoint:
|
|
|
|
| ?- joeEmployee::getOlder.
|
|
|
|
yes
|
|
|
|
|
|
% we can check this by asking joe its age:
|
|
|
|
|
|
| ?- joePerson::age(Age).
|
|
|
|
Age = 32
|
|
yes
|
|
|
|
|
|
% as you can see, although the modification message have been sent to a
|
|
% descendant, its the predicate age/1 in the parent that got updated
|
|
|
|
|
|
% to illustrate value sharing we use a couple of predicates, score/1 and
|
|
% setScore/0, defined in joePerson:
|
|
|
|
|
|
| ?- joePerson::score(Score).
|
|
|
|
Score = 0
|
|
yes
|
|
|
|
|
|
% initially, score/1 is only defined for joePerson, so every descendant
|
|
% or viewpoint will share its value/definition:
|
|
|
|
| ?- joeEmployee::score(Score).
|
|
|
|
Score = 0
|
|
yes
|
|
|
|
|
|
% but if we decide to increment the counter by sending the setScore/0
|
|
% message to a descendant:
|
|
|
|
| ?- joeChessPlayer::(setScore(2200), score(Score)).
|
|
|
|
Score = 2200
|
|
yes
|
|
|
|
|
|
% then the descendant will now have a local definition for counter/1,
|
|
% independent of the definition in its parent, joePerson:
|
|
|
|
| ?- joePerson::score(Score).
|
|
|
|
Counter = 0
|
|
yes
|
|
|
|
|
|
% the other descendants/viewpoints will continue to share the definition
|
|
% in joePerson:
|
|
|
|
| ?- joeSportsman::score(Score).
|
|
|
|
Counter = 0
|
|
yes
|