【发布时间】:2015-05-24 08:06:23
【问题描述】:
所以我有课
(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? IE。无需列出所有插槽:
(defun make-bar-from-foo (existing-foo c)
(make-instance 'bar :a (a existing-foo) :b (b existing-foo) :c c))
【问题讨论】:
-
没有内置任何东西。您确定不想只使用
CHANGE-CLASS将existing-foo更改为bar? -
@Barmar 哦不知道
CHANGE-CLASS,谢谢! -
CLOS 本身没有构造函数。如果你想要的是一个新的
bar对象,change-class可能不是你想要的,因为它实际上改变了对象。 -
@Barmar 你们想提交这个作为答案吗?谢谢
-
嗯,这取决于您真正需要完成的工作。正如@Vatine 所说,如果您使用
CHANGE-CLASS,您不会得到新对象,它会修改原始对象。您可以复制foo,然后更改其类,但 CLOS 没有内置的复制操作,因此您必须对其进行编码(它类似于make-bar-from-foo,只是它不会' t 添加c槽。
标签: common-lisp clos