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

Methods

Defining a method and creating a generic function

By using defmethod to define a method, you automatically generate a generic function with the same name as the method. For example, you can create a generic function #'say by using defmethod to define a method say. Here is an example of the use of defmethod.

The following function prints a sentence of an instance of fourth-grader surrounded by enthusiastic punctuation, then returns nil:

? (defmethod say ((child fourth-grader) sentence)
    (princ "***")
    (princ sentence)
    (princ "!***")
    (terpri))
SAY
? (setq billy-crystal (make-instance 'fourth-grader
                        :name "Billy Crystal"))
#<FOURTH-GRADER #x60B5F1>

When say is called on billy-crystal, it checks the type of billy-crystal and finds that it is a fourth-grader. The generic function checks to see whether it has a method for fourth-grader; since it does, it runs that method.

? (say billy-crystal "Marvelous")
***Marvelous!***
NIL


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

Generated with Harlequin WebMaker