Include Paulo Moura's Logtalk OO LP system
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@53 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
15
Logtalk/examples/classvars/NOTES
Normal file
15
Logtalk/examples/classvars/NOTES
Normal file
@@ -0,0 +1,15 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
To load all objects in this example consult the classvars.loader utility
|
||||
file (note that the *.loader files are Prolog files).
|
||||
|
||||
This folder contains an example that shows how to implement class variables
|
||||
as defined in Smalltalk. The name shared instance variables is however much
|
||||
more accurate. In systems like Logtalk that enable the use of explicit
|
||||
metaclasses, true class variables are just the class (as an object) own
|
||||
instance variables!
|
32
Logtalk/examples/classvars/SCRIPT
Normal file
32
Logtalk/examples/classvars/SCRIPT
Normal file
@@ -0,0 +1,32 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
% get the value of the class variable for each instance:
|
||||
|
||||
| ?- instance1::cv(Value1), instance2::cv(Value2), instance3::cv(Value3).
|
||||
|
||||
Value1 = 0
|
||||
Value2 = 0
|
||||
Value3 = 0
|
||||
yes
|
||||
|
||||
|
||||
% change the value of the class variable via instance1:
|
||||
|
||||
| ?- instance1::set_cv(1).
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% get the value of the class variable for the other instances:
|
||||
|
||||
| ?- instance2::cv(Value2), instance3::cv(Value3).
|
||||
|
||||
Value2 = 1
|
||||
Value3 = 1
|
||||
yes
|
7
Logtalk/examples/classvars/classvars.loader
Normal file
7
Logtalk/examples/classvars/classvars.loader
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
root,
|
||||
instance1,
|
||||
instance2,
|
||||
instance3])).
|
6
Logtalk/examples/classvars/instance1.lgt
Normal file
6
Logtalk/examples/classvars/instance1.lgt
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
:- object(instance1,
|
||||
instantiates(root)).
|
||||
|
||||
|
||||
:- end_object.
|
6
Logtalk/examples/classvars/instance2.lgt
Normal file
6
Logtalk/examples/classvars/instance2.lgt
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
:- object(instance2,
|
||||
instantiates(root)).
|
||||
|
||||
|
||||
:- end_object.
|
6
Logtalk/examples/classvars/instance3.lgt
Normal file
6
Logtalk/examples/classvars/instance3.lgt
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
:- object(instance3,
|
||||
instantiates(root)).
|
||||
|
||||
|
||||
:- end_object.
|
29
Logtalk/examples/classvars/root.lgt
Normal file
29
Logtalk/examples/classvars/root.lgt
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
:- object(root,
|
||||
instantiates(root)).
|
||||
|
||||
|
||||
:- private(cv_/1).
|
||||
:- dynamic(cv_/1).
|
||||
:- mode(cv_(?integer), zero_or_one).
|
||||
|
||||
:- public(cv/1).
|
||||
:- mode(cv(?integer), zero_or_one).
|
||||
|
||||
:- public(set_cv/1).
|
||||
:- mode(set_cv(+integer), one).
|
||||
|
||||
|
||||
cv_(0). % cv value is stored locally, in this class
|
||||
|
||||
|
||||
cv(Value) :-
|
||||
cv_(Value). % retrive cv value, shared for all instances
|
||||
|
||||
|
||||
set_cv(Value) :-
|
||||
retractall(cv_(_)), % retract old cv value from this class
|
||||
asserta(cv_(Value)). % assert the new value in this class
|
||||
|
||||
|
||||
:- end_object.
|
Reference in New Issue
Block a user