The following is an example of a Carapace object.
This Trumpet object describes a trumpet (ie. the musical instrument)
which can be muted (ie. have a mute stuffed in the end to change the sound) or open
(no mute).
(class "Trumpet"
(interface
(properties
(mute String)
(metal String readOnly)
)
(methods
# constructor function - used when creating a Trumpet
(NIL Trumpet ( (m String) ) )
# play a piece of music
(NIL play ( (m Music) ) )
)
)
)
Points to note:
Trumpet
NIL and, in this example,
takes a single argument which is the type of metal from which the trumpet
is made.
Music.
readOnly meaning that it cannot be altered
by anyone accessing the property directly. In Carapace, readOnly attributes can
only be changed by the object methods. In this example, the type of metal is
set when the Trumpet is created - the same as in the real world
Our Trumpet object is pretty good - it can already play music - but
suppose the people living in the same house wanted some sort of control over
the trumpet. These people don't want to play the trumpet - they just want to
control its volume. This could be done by having an additional interface on our
Trumpet object eg.:
(interface
(methods
# set the maximum volume
(NIL setVolume ( (max Integer) ) )
# make it silent!
(NIL silencer () )
)
)
Putting it all together, the full class declaration looks like:
(class "Trumpet"
(interface
(properties
(mute String)
(metal String readOnly)
)
(methods
# initialise the object
(NIL Trumpet ( (m String) ) )
# play a piece of music
(NIL play ( (m String) ) )
)
)
(interface Volume
(methods
# set the maximum volume
(NIL setVolume ( (max Integer) ) )
# make it silent!
(NIL silencer () )
)
)
)
To see how to create and access a Trumpet object, click
here.
For a complete description of object definition, click here
Trumpet via COMBy adding a few more details to our Trumpet example above, we can access it from COM.
To see what is required, click here.
| Contents | Index | Current topic: objects | Related topics: functions |