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

Method combination

Examples of classes with multiple superclasses

We have seen simple class precedence at work in earlier examples. Let us look at some examples of multiple superclasses.

For example, suppose two classes, bored-kid and happy-kid, both subclasses of fourth-grader, with instances harvey and laura. The bored kid has homework and the happy kid has a pet. Each subclass has a method for a generic function, #'go-crazy:

? (defclass bored-kid (fourth-grader)
    ((homework :initarg :homework
               :initform "Easy Steps to Calculus"
               :accessor homework)))
#<STANDARD-CLASS BORED-KID>
? (defmethod go-crazy ((child bored-kid) exclamation)
    (princ "I'm so bored!  ")
    (princ exclamation)
    (princ "!  What a dump!")
    (terpri))
#<Method GO-CRAZY (BORED-KID T)>
?  (setq harvey (make-instance 'bored-kid))
#<BORED-KID #x608879>
? (go-crazy harvey "Yukh")
I'm so bored!  Yukh!  What a dump!
NIL
? (defclass happy-kid (fourth-grader)
    ((pet :initarg :pet
          :initform "gorilla"
          :accessor pet)))
#<STANDARD-CLASS HAPPY-KID>
? (defmethod go-crazy ((child happy-kid) exclamation)
    (princ "How happy I am!  ")
    (princ exclamation)
    (princ "!  I'm going to shout and throw paper clips!")
    (terpri))
#<Method GO-CRAZY (HAPPY-KID T)>
? (setq laura (make-instance 'happy-kid))
#<HAPPY-KID #x608C51>
? (go-crazy laura "Wow")
How happy I am!  Wow!  I'm going to shout and throw paper clips!
NIL

When there is a conflict: Choosing between methods
Choosing between methods associated with direct and with more distant superclasses

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

Generated with Harlequin WebMaker