Method combination
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
Generated with Harlequin WebMaker