2017-07-12 33 views
2

我有一个类消息的实例,我将称之为“味精”。我定义了一个类“my-message”,并希望实例“msg”现在成为该类。如何将一个实例“投”到一个子类?

这听起来像它应该是相对简单的,但我不知道该怎么做。改变班给了我一个我不明白的错误。

(defclass my-message (message) 
    ((account-name :accessor account-name :initform nil :initarg :account-name))) 

(change-class msg 'my-message :account-name account-name) 

ERROR : 
While computing the class precedence list of the class named MW::MY-MESSAGE. 
The class named MW::MESSAGE is a forward referenced class. 
The class named MW::MESSAGE is a direct superclass of the class named MW::MY-MESSAGE. 
+0

你说你有一个'msg'类的实例。在你的代码中,你使用了一个类“消息”。这个类的消息是在哪里定义的? –

回答

4
The class named MW::MESSAGE is a forward referenced class. 

正向引用类是引用但尚未定义的类。如果你看看班级的名字,那就是MW::MESSAGE。我想你想在另一个包中继承另一个名为MESSAGE的类;您输入的符号可能有问题。

The class named MW::MESSAGE is a direct superclass of the class named MW::MY-MESSAGE. 

由于MW::MESSAGE类没有定义,你不能做它的一个实例。这也是为什么你不能创建它的任何子类的实例,如MW::MY-MESSAGE

+1

这确实是一个符号问题。我错过了一个“前向引用类”。非常感谢 ! – Arnaud

4

这个工作对我来说:

CL-USER> (defclass message()()) 
#<STANDARD-CLASS COMMON-LISP-USER::MESSAGE> 

CL-USER> (defparameter *msg* (make-instance 'message)) 
*MSG* 

CL-USER> (describe *msg*) 
#<MESSAGE {1002FE43F3}> 
    [standard-object] 
No slots. 


CL-USER> (defclass my-message (message) 
      ((account-name :accessor account-name 
          :initform nil 
          :initarg :account-name))) 
#<STANDARD-CLASS COMMON-LISP-USER::MY-MESSAGE> 

CL-USER> (change-class *msg* 'my-message :account-name "foo") 
#<MY-MESSAGE {1002FE43F3}> 

CL-USER> (describe *msg*) 
#<MY-MESSAGE {1002FE43F3}> 
    [standard-object] 

Slots with :INSTANCE allocation: 
    ACCOUNT-NAME = "foo" 

请注意,这不是一个,因为对象本身将被改变。它现在是一个不同类别的实例。 铸造通常意味着在某些情况下,对未改变的事物的解释会发生变化。但这里的情况真的改变了,旧的解释不再适用。

相关问题