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

Examples of classes with multiple superclasses

When there is a conflict: Choosing between methods

When two classes inherit from the same superclasses, CLOS uses the class precedence list to determine which primary method to use. For example, we can define two new classes, happy-kid-with-homework and bored-kid-with-pet, that inherit from the same superclasses, but in opposite order.

? (defclass happy-kid-with-homework
    (happy-kid bored-kid)())
#<STANDARD-CLASS HAPPY-KID-WITH-HOMEWORK>
? (setq smiley (make-instance 'happy-kid-with-homework))
#<HAPPY-KID-WITH-HOMEWORK #x609C48>
?  (defclass bored-kid-with-pet (bored-kid happy-kid)())
#<STANDARD-CLASS BORED-KID-WITH-PET>
? (setq bad-max (make-instance 'bored-kid-with-pet))
#<BORED-KID-WITH-PET #x609C53>

To find the applicable method for go-crazy, CLOS looks for a method associated with the instance, then with the class, then with the first direct superclass in the list of the class's parents. The function go-crazy has an applicable method for the first parent, happy-kid. Therefore happy-kid-with-homework calls the go-crazy method associated with happy-kid as a primary method. It never calls the method for bored-kid because it doesn't get that far.

? (go-crazy smiley "Wow")
How happy I am! Wow! I'm going to shout and throw paper clips!
NIL

But bored-kid-with-pet calls the bored-kid method.

? (go-crazy bad-max "Ugh")
I'm so bored! Ugh! What a dump!
NIL


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

Generated with Harlequin WebMaker