Difference between revisions of "Object Oriented Programming in Script/Objects"
Line 18: | Line 18: | ||
| | ||
− | + | Objects | |
+ | |||
+ | |||
+ | Objects are specific instances of a class, and are defined using the instance statement: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (instance anObject of aClass | ||
+ | (properties | ||
+ | aProperty: value | ||
+ | ... | ||
+ | ) | ||
+ | |||
+ | (method (aMethod [p1 p2 ...] [&tmp t1 t2 ...]) | ||
+ | code | ||
+ | ) | ||
+ | ... | ||
+ | |||
+ | [(procedure ...)] | ||
+ | )</syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | This defines an Object as an instance of class aClass. The properties and method statements are optional and are used to over-ride the default values and methods inherited from aClass. | ||
+ | |||
+ | The ID of an object or a class is what tells the sci kernel where to send messages. The object ID is obtained by simply writing the object's name wherever an expression is valid -- it can be assigned to a variable or passed as a parameter to a procedure. Thus, if we define egoObj as an instance of class Ego, | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (instance egoObj of Ego) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | we can assign the ID of egoObj to the global variable ego: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (global ego 0) | ||
+ | (= ego egoObj) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | Once this has been done, the following two expressions are equivalent: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (ego x?) | ||
+ | (egoObj x?) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | To find the distance from the object ego to the object wolf, we can pass the wolf's ID as an argument to ego's distanceTo: method: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (ego distanceTo: wolf) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | Any unknown symbol encountered in a compilation is assumed to be the ID of some object which is to be defined later in the source file. If no object with the symbol as its name is encountered by the end of the file, an error will be raised. | ||
| |
Revision as of 20:29, 5 December 2015
Objects
Objects are specific instances of a class, and are defined using the instance statement:
Code:(instance anObject of aClass (properties aProperty: value ... ) (method (aMethod [p1 p2 ...] [&tmp t1 t2 ...]) code ) ... [(procedure ...)] )
This defines an Object as an instance of class aClass. The properties and method statements are optional and are used to over-ride the default values and methods inherited from aClass.
The ID of an object or a class is what tells the sci kernel where to send messages. The object ID is obtained by simply writing the object's name wherever an expression is valid -- it can be assigned to a variable or passed as a parameter to a procedure. Thus, if we define egoObj as an instance of class Ego,
Code:(instance egoObj of Ego)
we can assign the ID of egoObj to the global variable ego:
Code:(global ego 0) (= ego egoObj)
Once this has been done, the following two expressions are equivalent:
Code:(ego x?) (egoObj x?)
To find the distance from the object ego to the object wolf, we can pass the wolf's ID as an argument to ego's distanceTo: method:
Code:(ego distanceTo: wolf)
Any unknown symbol encountered in a compilation is assumed to be the ID of some object which is to be defined later in the source file. If no object with the symbol as its name is encountered by the end of the file, an error will be raised.
- Notes
< Previous: Classes Next: Sending Messages >