使用超类构造函数?

时间:2015-05-24 08:06:24

标签: common-lisp clos

所以我有课程

...-20150521.010136-r.jar

和构造函数

(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

1 个答案:

答案 0 :(得分: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.