Common lisp 使用超类构造函数?

Common lisp 使用超类构造函数?,common-lisp,clos,Common Lisp,Clos,所以我有课 (defclass foo () ((a :initarg :a :accessor a) (b :initarg :b :accessor b))) (defclass bar (foo) ((c :initarg :c))) 还有一个构造器 (defun make-foo (a b) (make-instance 'foo :a a :b b)) 是否有一种简单的方法来定义一个函数,该函数接受现有的FOO并生成一个定义了额外插槽C的条?即,无需列出所有插槽

所以我有课

(defclass foo ()
  ((a :initarg :a :accessor a)
   (b :initarg :b :accessor b)))

(defclass bar (foo)
  ((c :initarg :c)))
还有一个构造器

(defun make-foo (a b)
  (make-instance 'foo :a a :b b))
是否有一种简单的方法来定义一个函数,该函数接受现有的FOO并生成一个定义了额外插槽C的条?即,无需列出所有插槽:

(defun make-bar-from-foo (existing-foo c)
  (make-instance 'bar :a (a existing-foo) :b (b existing-foo) :c c))

这可能是一种选择:

(defclass foo ()
  ((a :initarg :a :accessor a)
   (b :initarg :b :accessor b)
   (initargs :accessor initargs))) ;Remember the initargs here.

(defclass bar (foo)
  ((c :initarg :c :accessor c)))

(defmethod initialize-instance :after ((foo foo) &rest initargs)
  (setf (initargs foo) initargs))

(defun make-bar-from-foo (foo c)
  (apply #'make-instance 'bar :c c (initargs foo))) ;And use them here.

没有内置的东西。你确定不想仅仅使用CHANGE-CLASS将现有的foo更改为酒吧吗?@Barmar oh不知道CHANGE-CLASS,谢谢!CLOS本身没有构造函数。如果您想要的是一个新的bar对象,那么change类可能不是您想要的,因为它实际上改变了对象。@Barmar你们想提交它作为答案吗?谢谢好吧,这取决于你真正需要完成什么。正如@Vatine所说,如果你使用CHANGE-CLASS,你不会得到一个新的对象,它会修改原来的对象。您可以复制foo,然后更改其类,但CLOS没有内置的复制操作,因此您必须编写代码,使其类似于从foo生成bar,只是它不会添加c插槽。