1

所以我有课

(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并产生一个定义BAR了额外插槽的C函数?即无需列出所有插槽:

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

1 回答 1

0

这可能是一个选项:

(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.
于 2015-08-17T09:12:13.393 回答