[Next] [Previous] [Up] [Top] [Contents] [Index]

Classes and instances

Creating a class with the macro defclass

The macro defclass creates a new class. Its first argument is a list of the superclasses of the new class. If the argument is nil, the new class is based on standard-object.

Its second argument is a list of slot specifiers. Each slot is a list, the first element of which is a symbol that names the slot. The second and third elements are the slot's initialization argument and default initial value form, if it has them. These appear in definitions as the initarg and initform keywords.

Initialization arguments are keyword arguments used to supply the values for slots when new instances are created. Initial value forms provide a mechanism for a user to give a default initial value form for a slot.

The macro defclass returns the new class object.

Here is an example of the use of defclass.

? (defclass fourth-grader () 
    ((teacher :initarg :teacher
              :initform "Mrs. Marple")
     (name :initarg :name)
     (age :initarg :age :initform 9)))
#<STANDARD-CLASS FOURTH-GRADER>

Figure C-1 shows a graphic representation of how the class fourth-grader is built. This class is associated with three slots--name, age, which has the default initial value 9, and teacher, which has the default initial value "Mrs. Marple". The name of the new class is the symbol fourth-grader.

Figure C-1 The class fourth-grader


Gettmg Started with MCL - 19 OCT 1996
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker