home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
proglang
/
pie2.arj
/
OBJECT.PRO
< prev
next >
Wrap
Text File
|
2000-01-01
|
2KB
|
70 lines
% call1/1
call1("true"):-
!.
call1(Method):-
call(Method).
% get_method/3
get_method(Message,[FirstMethod|Rest],Method):-
fact_or_rule(Message,FirstMethod,Method).
get_method(Message,[_|Rest],Method):-
get_method(Message,Rest,Method).
% fact_or_rule/3
fact_or_rule(Message,Message,"true").
fact_or_rule(Message,(Message :- Body),Body).
% isa/2
isa(klingon(X_Velocity,Y_Velocity,Mass),spaceship(X_Velocity,Y_Velocity,Mass)).
isa(enterprise(X_Velocity,Y_Velocity,Mass),spaceship(X_Velocity,Y_Velocity,Mass)).
% isa_hierarchy/2
isa_hierarchy(Object,Object).
isa_hierarchy(Object,Object1):-
isa(Object,Object2),
isa_hierarchy(Object2,Object1).
% send/2
send(Object,Message):-
isa_hierarchy(Object,Object1),
object(Object1,Methodlist),
get_method(Message,Methodlist,Method),
call1(Method).
% st/1
st(E):-
concat("Example of object"," oriented programming.",E),
write("Finished method for spaceship."),
nl.
% start/0
start():-
write("Sending message to Enterprise..."),
send(enterprise(20,30,40),kinetic_energy(E)),
nl,
write("Energy is "),
write(E),
nl,
write("Sending description message to Klingon..."),
send(klingon(_,_,_),description(D)),
nl,
write("Klingon is "),
write(D),
nl,
send(spaceship(A),ee(A)),
write(A),
nl.
% object/2
object(spaceship(E),[(ee(E):-st(E))]).
object(spaceship(X_Velocity,Y_Velocity,Mass),
[(kinetic_energy(E) :- E is 0.5 * Mass * (X_Velocity * X_Velocity + Y_Velocity * Y_Velocity)),
(speed(S) :- S is sqrt(X_Velocity * X_Velocity + Y_Velocity * Y_Velocity)),
description("a space ship")]).
object(enterprise(X_Velocity,Y_Velocity,Mass),[description("a Federation Star Ship")]).
object(klingon(X_Velocity,Y_Velocity,Mass),[description("an enemy Star Ship")]).
object(romulan(X,Y,M),[description("an enemy Star Ship")]).