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" 

請注意,這不是一個,因爲對象本身將被改變。它現在是一個不同類別的實例。 鑄造通常意味着在某些情況下,對未改變的事物的解釋會發生變化。但這裏的情況真的改變了,舊的解釋不再適用。

相關問題