我需要一些帮助来理解以下代码的执行顺序。
我使用以下内容创建了一个实例pie
:
(cook (make-instance 'pie))
我知道 lisp 执行从最具体到最不具体的函数。但是,它看起来不像在(defmethod cook ((p pie))
被调用之后被遵循。
我会假设 (defmethod cook :after ((f food))
&(defmethod cook :after ((p pie))
以相反的顺序执行,因为我们的实例是pie
,而不是父类,food
。
谢谢,任何输入将不胜感激。
(defclass food () ())
(defmethod cook :before ((f food))
(print "A food is about to be cooked."))
(defmethod cook :after ((f food))
(print "A food has been cooked."))
(defclass pie (food)
((filling :accessor pie-filling
:initarg :filling
:initform 'apple)))
(defmethod cook ((p pie))
(print "Cooking a pie.")
(setf (pie-filling p) (list 'cooked (pie-filling p))))
(defmethod cook :before ((p pie))
(print "A pie is about to be cooked."))
(defmethod cook :after ((p pie))
(print "A pie has been cooked."))
(setq pie-1 (make-instance 'pie :filling 'apple))
输出如:
"A pie is about to be cooked."
"A food is about to be cooked."
"Cooking a pie."
"A food has been cooked."
"A pie has been cooked."
(COOKED APPLE)