【问题标题】:Equivalance of instanceof for CLOS? How to check if instance is inherited from another object?相当于 CLOS 的 instanceof?如何检查实例是否继承自另一个对象?
【发布时间】:2013-10-09 16:50:10
【问题描述】:
CL-USER> (defclass a () ())
CL-USER> (defclass b (a) ())
CL-USER> (make-instance 'b)
#<STANDARD-CLASS B>

我可以在我的实例 b 上调用什么谓词函数,如果它是从 a 继承的,它会返回 T?在脉络中:

CL-USER> (instanceof 'a *)
T

【问题讨论】:

    标签: inheritance common-lisp clos


    【解决方案1】:

    类名也是类型名,所以:

    (typep * 'a)
    

    请参阅集成类型和类http://clhs.lisp.se/Body/04_cg.htm

    或者你可以这样做:

    (defmethod is-an-a-p ((x a))
      t)
    (defmethod is-an-a-p ((x t))
      nil)
    

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 2011-03-06
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多