Class 如果Lisp中没有定义超类状态,这意味着什么?

Class 如果Lisp中没有定义超类状态,这意味着什么?,class,common-lisp,superclass,clos,Class,Common Lisp,Superclass,Clos,我的超类定义如下: (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 :missionar

我的超类定义如下:

(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
错误:类#
无法完成,因为超类状态为
还没有定义

从的文档中可以清楚地看到,通过扩展父类
状态
,您正在创建
传教士状态

现在,错误消息指示您在定义
传教士状态
的父类
状态
之前,尝试创建
传教士状态
的实例。它不是一个标准类,所以您需要告诉系统这是什么

如果您不希望它成为
state
的子类,只需删除定义中的父类即可:

(defclass missionary-state ()
  ...)
现在它是
标准对象
标准类
的直接子类

我的超类定义如下:

(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")))
(高级传教士州)

这不是一个超类。至少我们不知道它是一个。如果你从这个类继承,它可能是一个。你要做的是:

  • 定义一个类
    传教士状态
  • 该类有一个超类
    状态
  • 超类
    状态
    不是示例的一部分,因此未定义
Common Lisp允许您定义具有未定义超类的类。您甚至不需要告诉Common Lisp
state
是一个类,它将在以后定义

但是,在创建该类或其子类的实例之前,需要定义该类的
state
。错误消息告诉您Lisp无法完成该类的
传教士状态
,因为该类
状态
尚不存在-名称已知,但没有定义

通常,您只需定义类
状态
,然后创建实例。如果可能,那么您也可以这样做:

  • 定义类
    传教士状态
    ,而不将
    状态
    列为超类
  • 创建类的实例
    传教士状态
  • 定义类
    状态
  • 重新定义类
    传教士状态
    以拥有超类
    状态

然后,这将更新现有实例,并确保将来的实例使用更新的类(包括超类)中的信息。这是可能的,因为CLOS(公共Lisp对象系统)允许对类图进行各种运行时更新。

什么是
defclass传教士状态(state)
应该做什么?定义传教士状态类。感谢您明确的回答。我意识到我在加载文件之前忘记加载和编译另一个文件。这就是为什么我的超类状态未定义。在您的最后一句话中,它指的是什么?@Michelle如果您创建一个父类列表为空,它将始终继承from
标准对象
标准类
。如果父列表是
(state)
,则state类是父类,它很可能将前面提到的类作为其父类。标准对象和标准类有什么?@Michelle See和