Inheritance 检查对象的父类

Inheritance 检查对象的父类,inheritance,lisp,common-lisp,clos,Inheritance,Lisp,Common Lisp,Clos,我想知道一种方法,如何检查一个对象是否属于某个类,或者是从它派生的。例如: (defclass a nil nil) (defclass b (a) nil) (defparameter *foo* (make-instance 'b)) (my-function *foo* 'a) ; => t (my-function *foo* 'b) ; => t 或者,也可以使用返回给定对象(或类)的所有基类列表的函数。使用: 使用: 您需要使用MOP(元对象协议)`类直接

我想知道一种方法,如何检查一个对象是否属于某个类,或者是从它派生的。例如:

(defclass a nil
  nil)

(defclass b (a)
  nil)

(defparameter *foo* (make-instance 'b))

(my-function *foo* 'a) ; => t
(my-function *foo* 'b) ; => t
或者,也可以使用返回给定对象(或类)的所有基类列表的函数。

使用:

使用:

您需要使用MOP(元对象协议)`类直接超类'

快速加载库closer mop并使用“类直接超类”,如下所示:

CL-USER> (closer-mop:class-direct-superclasses (find-class 'number))
(#<BUILT-IN-CLASS T>)
CL-USER>
CL-USER>(closer-mop:类直接超类(查找类的编号))
(#)
CL-USER>
如果你有一个类的实例,你可以

CL-USER> (let ((table (make-instance 'test-table-2)))
           (class-direct-superclasses (class-of table)))
(#<STANDARD-CLASS STANDARD-OBJECT>)} 
CL-USER>
CL-USER>(let((表(make-instance'test-table-2)))
(类直接超类(表的类)))
(#)} 
CL-USER>
一个可能的问题(记录在库中):如果您取消打包:使用closer mop,而不是使用CL,:使用closer common lisp,您需要使用mop(元对象协议)`class direct Superclass'

快速加载库closer mop并使用“类直接超类”,如下所示:

CL-USER> (closer-mop:class-direct-superclasses (find-class 'number))
(#<BUILT-IN-CLASS T>)
CL-USER>
CL-USER>(closer-mop:类直接超类(查找类的编号))
(#)
CL-USER>
如果你有一个类的实例,你可以

CL-USER> (let ((table (make-instance 'test-table-2)))
           (class-direct-superclasses (class-of table)))
(#<STANDARD-CLASS STANDARD-OBJECT>)} 
CL-USER>
CL-USER>(let((表(make-instance'test-table-2)))
(类直接超类(表的类)))
(#)} 
CL-USER>
一个可能的问题(记录在库中):如果您取消打包:使用closer mop,而不是使用CL,:使用closer common lisp

的可能重复