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

Methods

Defining methods on instances

You can specialize methods on individual instances as well as on entire classes. If you need a method for say that is called only on alisa, an instance of fourth-grader, then specify child to be eql to alisa, as shown in the following example. The expression (declare (ignore sentence)) avoids a compiler warning. The function call-next-method calls the next most specific method, which is the method on fourth-grader:

? (defmethod say ((child (eql alisa)) sentence)
    (declare (ignore sentence)) 
    (call-next-method)
    (princ "I have an HP calculator")
    (terpri))
#<Method SAY ((EQL #<FOURTH-GRADER #x436B51> T)>

? (say alisa "Hello")
***Hello!***
I have an HP calculator
NIL

Applied to other fourth-graders, such as sharon, say still calls its usual fourth-grader method.

? (say sharon "Hello")
***Hello!***
NIL

* Note: The function call-next-method is perfectly acceptable here, but CLOS more commonly would use an :after method. Method combination is discussed later in "Creating auxiliary methods and using method qualifiers" on page 100.


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

Generated with Harlequin WebMaker