CLOS 中 (:before/:after) 方法调用的顺序?

发布于 2024-12-19 15:25:54 字数 1132 浏览 2 评论 0原文

我需要一些帮助来理解以下代码的执行顺序。

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀手六號 2024-12-26 15:25:54

请参阅Common Lisp HyperSpec 第 7.6.6.2 节(标准方法组合) 。这是最相关的段落:

before 方法按照最具体优先的顺序运行,而
after 方法以最不具体​​的优先顺序运行。设计
可以用一个例子来说明这种差异的基本原理。
假设类 C1 修改其超类 C2 的行为:
添加 before 方法和 after 方法。其行为是否
类 C2 直接由 C2 上的方法定义或从其继承
超类不影响调用的相对顺序
类 C1 的实例上的方法。 C1 类的 before 方法运行
在类 C2 的所有方法之前。 C1 类的 after 方法在之后运行
C2 类的所有方法。

See section 7.6.6.2 (Standard Method Combination) of the Common Lisp HyperSpec. Here's the most relevant passage:

The before methods are run in most-specific-first order while the
after methods are run in least-specific-first order. The design
rationale for this difference can be illustrated with an example.
Suppose class C1 modifies the behavior of its superclass, C2, by
adding before methods and after methods. Whether the behavior of the
class C2 is defined directly by methods on C2 or is inherited from its
superclasses does not affect the relative order of invocation of
methods on instances of the class C1. Class C1's before method runs
before all of class C2's methods. Class C1's after method runs after
all of class C2's methods.

扭转时空 2024-12-26 15:25:54
  • 首先执行最具体的主要方法,然后通过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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文