| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
9.3 Instance Creation and Slot Access
An instance (or object) of a defined class can be created with
make. make takes one mandatory parameter, which is the
class of the instance to create, and a list of optional arguments that
will be used to initialize the slots of the new instance. For instance
the following form
(define c (make <my-complex>))
creates a new <my-complex> object and binds it to the Scheme
variable c.
- generic: make
- method: make (class <class>) . initargs
Create and return a new instance of class class, initialized using initargs.
In theory, initargs can have any structure that is understood by whatever methods get applied when the
initializegeneric function is applied to the newly allocated instance.In practice, specialized
initializemethods would normally call(next-method), and so eventually the standard GOOPSinitializemethods are applied. These methods expect initargs to be a list with an even number of elements, where even-numbered elements (counting from zero) are keywords and odd-numbered elements are the corresponding values.GOOPS processes initialization argument keywords automatically for slots whose definition includes the
#:init-keywordoption (see section init-keyword). Other keyword value pairs can only be processed by aninitializemethod that is specialized for the new instance’s class. Any unprocessed keyword value pairs are ignored.
- generic: make-instance
- method: make-instance (class <class>) . initargs
make-instanceis an alias formake.
The slots of the new complex number can be accessed using
slot-ref and slot-set!. slot-set! sets the value
of an object slot and slot-ref retrieves it.
(slot-set! c 'r 10) (slot-set! c 'i 3) (slot-ref c 'r) ⇒ 10 (slot-ref c 'i) ⇒ 3
The (oop goops describe) module provides a describe
function that is useful for seeing all the slots of an object; it prints
the slots and their values to standard output.
(describe c)
-|
#<<my-complex> 401d8638> is an instance of class <my-complex>
Slots are:
r = 10
i = 3
| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on February 3, 2012 using texi2html 5.0.
