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

Classes and instances

Classes as prototypes of other classes

As you can see from the example "Creating an instance and giving its slots values" on page 88, you can build classes from other classes. Subclasses may add slots or change the default initial value of a slot that already exists. Subclasses may also have their own methods (see the next section, "Methods").

This is the syntax for defining a new class. It defines modern-fourth-grader, built on fourth-grader, with an additional slot, computer, and a new default value for teacher.

? (defclass modern-fourth-grader 
    (fourth-grader)
    ((computer :initarg :computer
               :initform "Macintosh")
     (teacher  :initform "We use HyperCard stacks.")))
#<STANDARD-CLASS MODERN-FOURTH-GRADER>
? (setf mariah (make-instance 'modern-fourth-grader))
#<MODERN-FOURTH-GRADER #x51A209>
? (slot-value mariah 'teacher)
"We use HyperCard stacks."
? (slot-value mariah 'computer)
"Macintosh"
? (setf (slot-value mariah 'teacher)
        "We program our own in Macintosh Common Lisp.")
"We program our own in Macintosh Common Lisp."

The instance mariah inherits slots and values it does not redefine.

? (slot-value mariah 'school)
"Lawrence School"


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

Generated with Harlequin WebMaker