Files
lisplings/solutions/15_clos/clos6.lisp
T

41 lines
1.0 KiB
Common Lisp

;;; CLOS 6: CALL-NEXT-METHOD AND NEXT-METHOD-P (solution)
(defclass animal () ())
(defclass dog (animal) ())
(defclass cat (animal) ())
(defgeneric announce (a))
(defmethod announce ((a animal))
"I am an animal.")
(defgeneric purr (a))
(defmethod purr ((a animal))
"prrr-from-animal")
(defgeneric hiss (a))
(defmethod announce ((d dog))
(concatenate 'string (call-next-method) " Woof!"))
(defmethod purr ((c cat))
(if (next-method-p)
(call-next-method)
"Prrr"))
(defmethod hiss ((c cat))
(if (next-method-p)
(call-next-method)
"Prrr"))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "I am an animal. Woof!" (announce (make-instance 'dog)))
(check-equal "I am an animal." (announce (make-instance 'animal)))
(check-equal "prrr-from-animal" (purr (make-instance 'cat)))
(check-equal "Prrr" (hiss (make-instance 'cat)))