CLOS 中 (:before/:after) 方法调用的顺序?
我需要一些帮助来理解以下代码的执行顺序。
我创建了一个 pie
实例,使用以下内容:
(cook (make-instance 'pie))
我知道 lisp 执行从最具体到最不具体的函数。但是,它看起来不像是在 (defmethod Cook 之后执行的 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)
I need some help understanding the order of execution for the following code.
I create an instance of pie
, using the following:
(cook (make-instance 'pie))
I know lisp executes functions from most specific to least specific.. however, it doesn't look like that is being followed after (defmethod cook ((p pie))
is called.
I would assume (defmethod cook :after ((f food))
& (defmethod cook :after ((p pie))
to be executed in opposite order, since our instance is of pie
, and not the parent class, food
.
Thanks,
any input will be greatly appreciated.
(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))
With output such as :
"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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅Common Lisp HyperSpec 第 7.6.6.2 节(标准方法组合) 。这是最相关的段落:
See section 7.6.6.2 (Standard Method Combination) of the Common Lisp HyperSpec. Here's the most relevant passage:
首先执行最具体的主要方法,然后通过
CALL-NEXT-METHOD
执行下一个具体方法。:before
方法首先执行最具体的方法。:after
方法按最不具体优先执行。The primary methods are executed most-specific first, then the next specific via
CALL-NEXT-METHOD
.the
:before
methods are executed most-specific-first.the
:after
methods are execute least-specific-first.