Warm tip: This article is reproduced from serverfault.com, please click

common lisp-如何在带有多个关键参数CLOS的initialize-instance中使用call-next-method

(common lisp - How to use call-next-method in initialize-instance with multiple key arguments CLOS)

发布于 2020-11-27 20:08:34

如果我有两个班级,一个班级parent和一个班级child

(defclass parent ()
    ...)

(defclass child (parent)
    ...)

而且我为initialize-instance定义了2种不同的方法,但是孩子的方法接受了另一个参数,然后调用 call-next-method

(defmethod initialize-instance :after ((instance parent) &key)
    ...) ; Do things

(defmethod initialize-instance :after ((instance child) &key other-arg)
    ... ; Do things
    (call-next-method))

我得到了错误

There is no next method for the generic function
#<STANDARD-METHOD COMMON-LISP:INITIALIZE-INSTANCE (93)>
when called from method
#<STANDARD-METHOD COMMON-LISP:INITIALIZE-INSTANCE :AFTER (CHILD) {...}>
with arguments
 ....

Condition of type SB-PCL::NO-NEXT-METHOD-ERROR

显然,我不能使用提供的参数调用下一个方法吗?

Questioner
userEwen
Viewed
11
tfb 2020-11-28 08:53:40

标准方法组合(即你正在使用的方法)中,在方法没有下一个方法之后,因此call-next-method不能在其中使用:在方法组合全部按最特定-最后的顺序调用所有方法之后才适用。你被允许使用的场所call-next-method是主要场所和周边场所