Classes and instances
make-instance. This function creates and returns a new object based on its argument, which should be a class.
? (setq john (make-instance 'fourth-grader))
#<FOURTH-GRADER #x436B51>
An instance has slots as determined by its class and that class's superclasses.
You can set the values of an instance's slots when you create it. The following example creates an instance of fourth-grader and uses the initialization argument :teacher to override the default value for that slot:
? (setf john (make-instance 'fourth-grader
:teacher "Ms. Hsu"))
#<FOURTH-GRADER #x477181>
The function slot-value retrieves the value of a slot. This function takes two arguments, the class or instance and the name of the slot.
? (slot-value john 'teacher)
"Ms. Hsu"
You can set the value of most slots of an already created instance by using setf with the name of the slot, for example, to give the instance john its own name (Figure C-2) or to change the value of the :teacher slot.
? (setf (slot-value john 'name) "John")
"John"
? (slot-value john 'name)
"John"
? (setf (slot-value john 'teacher) "Ms. Miller")
"Ms. Miller"
? (slot-value john 'teacher)
"Ms. Miller"
* Note: Accessor methods provide a simpler syntax than slot-value does. For more details, see "Creating and using accessor methods" on page 94.
Figure C-2 An instance of fourth-grader with a value in the name slot

To find the slot value associated with an instance, CLOS first looks up the value associated with the slot at the instance level. If the slot is unbound at the instance level, CLOS looks up the value associated with the instance's class. If the slot is still unbound, CLOS looks for the value in the slot associated with the first class that is a member of the class's class precedence list, then looks in the slot of the second class, and so on until it finds one value, which it returns.
Generated with Harlequin WebMaker