如果 Lisp 中没有定义超类状态,这意味着什么?
What does it mean if a superclass state is not defined in Lisp?
我的超类定义如下:
(defclass missionary-state (state)
((missionary-left :initarg :missionary-left :initform nil :accessor missionary-left
:documentation "the number of missionaries on the left side of the river")
(missionary-right :initarg :missionary-right :initform nil :accessor missionary-right
:documentation "the number of missionaries on the right side of the river")
(cannibal-left :initarg :cannibal-left :initform nil :accessor cannibal-left
:documentation "the number of cannibals on the left side of the river")
(cannibal-right :initarg :cannibal-right :initform nil :accessor cannibal-right
:documentation "the number of cannibals on the right side of the river")
(boat-pos :initarg :boat-pos :initform nil :accessor boat-pos
:documentation "the side the boat is on")))
这是我尝试加载后收到的错误消息:
Error: Class #<STANDARD-CLASS MISSIONARY-STATE>
can't be finalized because superclass STATE
is not defined yet
从 defclass
的文档可以清楚地看出,您正在通过扩展父 class state
创建 missionary-state
。
现在错误消息表明您在定义 missionary-state
的父 class state
之前尝试创建 missionary-state
的实例。它不是标准的 class 所以你需要告诉系统那是什么。
如果您不打算成为 state
的子 class,您只需删除定义中的父 class:
(defclass missionary-state ()
...)
现在它是 standard-object
和 standard-class
的直接子 class。
My superclass is defined as follows:
(defclass missionary-state (state)
那不是超级class。至少我们不知道它是一个。如果您继承自此 class,它可能是一个。你做的是这样的:
- 定义一个class
missionary-state
- class有超class
state
- superclass
state
不是您示例的一部分,因此未定义。
Common Lisp 允许您定义 classes 具有未定义的 superclasses。您甚至不需要告诉 Common Lisp state
是一个 class 并且稍后将定义它。
但是在创建 class 或其子 class 实例之前,您需要定义 class state
。错误消息告诉您 Lisp 无法最终确定 class missionary-state
,因为 class state
尚不存在 - 名称已知,但没有定义为此。
通常您只需定义 class state
然后创建实例。如果不可能,那么你也可以这样做:
- 定义 class
missionary-state
而不将 state
列为超级 class。
- 创建 class
missionary-state
的实例
- 定义 class
state
- 重新定义 class
missionary-state
以获得超级class state
这将更新现有实例,并确保未来的实例使用更新后的 class 中的信息,包括。超级class。这是可能的,因为 CLOS(Common Lisp 对象系统)允许对 class 图进行各种运行时更新。
我的超类定义如下:
(defclass missionary-state (state)
((missionary-left :initarg :missionary-left :initform nil :accessor missionary-left
:documentation "the number of missionaries on the left side of the river")
(missionary-right :initarg :missionary-right :initform nil :accessor missionary-right
:documentation "the number of missionaries on the right side of the river")
(cannibal-left :initarg :cannibal-left :initform nil :accessor cannibal-left
:documentation "the number of cannibals on the left side of the river")
(cannibal-right :initarg :cannibal-right :initform nil :accessor cannibal-right
:documentation "the number of cannibals on the right side of the river")
(boat-pos :initarg :boat-pos :initform nil :accessor boat-pos
:documentation "the side the boat is on")))
这是我尝试加载后收到的错误消息:
Error: Class #<STANDARD-CLASS MISSIONARY-STATE>
can't be finalized because superclass STATE
is not defined yet
从 defclass
的文档可以清楚地看出,您正在通过扩展父 class state
创建 missionary-state
。
现在错误消息表明您在定义 missionary-state
的父 class state
之前尝试创建 missionary-state
的实例。它不是标准的 class 所以你需要告诉系统那是什么。
如果您不打算成为 state
的子 class,您只需删除定义中的父 class:
(defclass missionary-state ()
...)
现在它是 standard-object
和 standard-class
的直接子 class。
My superclass is defined as follows:
(defclass missionary-state (state)
那不是超级class。至少我们不知道它是一个。如果您继承自此 class,它可能是一个。你做的是这样的:
- 定义一个class
missionary-state
- class有超class
state
- superclass
state
不是您示例的一部分,因此未定义。
Common Lisp 允许您定义 classes 具有未定义的 superclasses。您甚至不需要告诉 Common Lisp state
是一个 class 并且稍后将定义它。
但是在创建 class 或其子 class 实例之前,您需要定义 class state
。错误消息告诉您 Lisp 无法最终确定 class missionary-state
,因为 class state
尚不存在 - 名称已知,但没有定义为此。
通常您只需定义 class state
然后创建实例。如果不可能,那么你也可以这样做:
- 定义 class
missionary-state
而不将state
列为超级 class。 - 创建 class
missionary-state
的实例
- 定义 class
state
- 重新定义 class
missionary-state
以获得超级classstate
这将更新现有实例,并确保未来的实例使用更新后的 class 中的信息,包括。超级class。这是可能的,因为 CLOS(Common Lisp 对象系统)允许对 class 图进行各种运行时更新。