《实用Common Lisp编程》第16-17章,面向对象细节补遗(1):辅助方法

在《实用common lisp编程》的十六和十七章里面,介绍了common lisp的面向对象支持方式,分别是广义函数和CLOS系统。

 

书本通过文字叙述得非常详细,但是没有附上相应的代码,本着“没有实验过就没有发言权”的求实态度,我决定从带修饰符的辅助方法、带继承的方法、继承和槽、以及多继承等几个主要知识点着手,在代码方面实现一遍,验证书中的内容。

 

带辅助方法的主方法

 

第十六章介绍了几个广义函数的修饰符,主要有 :around , :before 和 :after ,而被装饰符修饰的方法称之为“主方法”(primary method),它们的一般运行顺序如下:

 

1.如果被调用的主方法有 :around 辅助方法,先运行 :around ,在 :around 运行之后,有两种选择,一种是,不调用 call-next-method ,那么整个函数就宣告结束,主方法不会被运行。

 

另一方面,如果 :around 调用 call-next-method ,这时又出现两种情况:

1) 该主方法还有一个最相关的 :around 辅助方法,那么运行该 :around 函数。(一般是父类的 :around)

2) 该主方法没有最想关的 :around 辅助方法,那么函数的执行权交还给主方法,主方法按正常情况运行,也即是,先运行 :before (如果有的话) ,再运行主方法,最后运行 :after (如果有的话)。

 

2.如果被调用的主方法(及父类的同名主方法)有 :before 辅助方法,那么它们按照最相关的最先运行的顺序依次执行,其中,不必调用 call-next-method 。

 

3.如果被调用的主方法(及其父类的同名主方法)有 :after 辅助方法,那么它们按照最相关的最后运行的顺序依次执行,其中,不必调用 call-next-method 。

 

4.如果主方法没有辅助方法,或者它的辅助方法全部执行完毕,那么执行该主方法。

 

OK,基本规则就这么多,我们先从 :before 辅助方法开始。

 

 

:before 辅助方法,无继承

 

无继承的 :before 辅助方法将在主方法之前执行,而且, :before 辅助方法执行完毕之后,调用会自动转给主方法,因此,不必调用 call-next-method 。

 

 

(defgeneric greet (people))

(defclass person ()
    ())
    
(defmethod greet ((people person))
    (format t "greet ~%"))

(defmethod greet :before ((people person))
    (format t "greet :before ~%"))
 

 

以下正是我们想要的执行结果:

 

 

(greet (make-instance 'person))

greet :before 

greet 

NIL

 

 

 

:before 辅助方法,带继承

 

这次,我们用一个 animal 类作为 person 的父类,调用 greet 方法。

 

如果一切如常的话,它们的调用顺序应该是 person 类的 greet 的 :before 辅助函数,然后是 animal 类的 greet 的 :before 辅助函数,然后是 person 的 greet 方法。

 

 

(defgeneric greet (people))

(defclass animal ()
    ())
    
(defmethod greet :before ((people animal))
    (format t "animal's greet :before ~%"))

(defclass person (animal)
    ())
    
(defmethod greet ((people person))
    (format t "greet ~%"))

(defmethod greet :before ((people person))
    (format t "greet :before ~%"))

 

 

 执行结果:

 

 

(greet (make-instance 'person))

greet :before 

animal's greet :before 

greet 

NIL

 

 

OK,方法如我们所料般执行。

 

我们可以来讲讲 :after 辅助方法了。

 

 

:after 辅助方法,无继承

 

无继承的带 :after 的辅助函数,将在主方法之后执行。

 

 

(defgeneric greet (people))

(defclass person ()
    ())
    
(defmethod greet ((people person))
    (format t "greet ~%"))
    
(defmethod greet :after ((people person))
    (format t "greet :after ~%"))

 

执行:

 

(greet (make-instance 'person))

greet 

greet :after 

NIL


好极了,下一个。


:after 辅助方法,有继承

这次,我们继续继承 animal 类,并且 animal 类带有一个 greet 方法的 :after 辅助方法。

按书本所说,最不相关的 :after 最先运行,因此,它们的运行顺序应该是: person 类的 greet 方法,animal 类的 greet 方法的 :after 辅助方法, person 类的 greet 方法的 :after 辅助方法。

(defgeneric greet (people))

(defclass animal ()
    ())

(defmethod greet :after ((people animal))
    (format t "animal's greet :after ~%"))

(defclass person (animal)
    ())
    
(defmethod greet ((people person))
    (format t "greet ~%"))
    
(defmethod greet :after ((people person))
    (format t "greet :after ~%"))
 
测试:

(greet (make-instance 'person))
greet 
animal's greet :after 
greet :after 
NIL

OK,两个 :after 辅助方法, 第二相关的 animal 类的辅助方法先运行,然后才是第一相关的 person 类本身的辅助方法运行。

接着,来看看 :around 辅助方法。


:around 无继承,不调用 call-next-method

按书上所说,:arond 方法将在主方法以及其他辅助方法之前运行,它的优先级是最高的,而且,只有当 :around 调用 call-next-method 时,方法的执行权才会转回给主方法。

我们这里用一个无继承,不调用 call-next-method 的 ;around 作测试,如果一切正常的话,那么,它只会执行 :around 辅助方法,而不执行主方法或者其他辅助函数。

(defgeneric greet (people))

(defclass person ()
    ())
    
(defmethod greet ((people person))
    (format t "greet ~%"))

(defmethod greet :around ((people person))
    (format t ":around ~%"))

(defmethod greet :before ((people person))
    (format t ":before ~%"))
    
(defmethod greet :after ((people person))
    (format t ":after ~%"))
 
执行试试:

(greet (make-instance 'person))
:around 
NIL

WOW,这个 :around 拦截了所有其他方法,只有它自己运行了,真够霸道的,看来用它写一些诸如 memorize 函数的东西应该不错。


:around 无继承,调用 call-next-method

这次,我们在 :around 函数体内加上 (call-next-method) ,这样,函数的执行权就会转交回给被装饰的 greet 函数,那么函数就会以一般的执行顺序执行(:before -> 主方法 -> :after)。

(defgeneric greet (people))

(defclass person ()
    ())
    
(defmethod greet ((people person))
    (format t "greet ~%"))

(defmethod greet :around ((people person))
    (format t ":around ~%")
    (call-next-method)) ; new add

(defmethod greet :before ((people person))
    (format t ":before ~%"))
    
(defmethod greet :after ((people person))
    (format t ":after ~%"))
 
测试:

(greet (make-instance 'person))
:around 
:before 
greet 
:after 
NIL

嗯,先 :around ,后 :before ,接着 greet 主方法 ,最后 :after ,这就是我们要的。


:around 有继承,父子类都有 :around,都调用 call-next-method

嗯,现在我们有两个类 animal 和 person ,它们都有 :around ,而且两个 :around 都在函数体最后调用 call-next-method 。

按书本所说,它们的执行顺序应该是: person 的greet方法的 :around , animal 的 greet 方法的 :around ,然后是 person 的 greet 方法的 :before ,person 的 greet 主方法,最后是 person 的 greet 方法的 :after 。

(defgeneric greet (people))

(defclass animal ()
    ())

(defmethod greet :around ((people animal))
    (format t "animal's greet :around ~%")
    (call-next-method)) ; call person's greet :before

(defclass person (animal)
    ())

(defmethod greet ((people person))
    (format t "greet ~%"))

(defmethod greet :around ((people person))
    (format t ":around ~%")
    (call-next-method)) ; call animal's greet :around

(defmethod greet :before ((people person))
    (format t ":before ~%"))

(defmethod greet :after ((people person))
    (format t ":after ~%"))

测试:

(greet (make-instance 'person))
:around 
animal's greet :around 
:before 
greet 
:after 

嗯,一切正常。



:around ,带继承,父类的 :around 不调用 call-next-method 方法

(defgeneric greet (people))

(defclass animal ()
    ())

(defmethod greet :around ((people animal))
    (format t "animal's greet :around ~%"))
    ; no (call-next-method) anymore

(defclass person (animal)
    ()) 

(defmethod greet ((people person))
    (format t "greet ~%"))

(defmethod greet :around ((people person))
    (format t ":around ~%")
    (call-next-method)) ; call animal's greet :around

(defmethod greet :before ((people person))
    (format t ":before ~%"))

(defmethod greet :after ((people person))
    (format t ":after ~%"))
 
还有一点小疑问,就是,如果我们在 animal 类的 greet 方法的 :around 辅助方法里不调用 call-next-method ,会如何?

答案是, animal 类的 greet 方法的 :around 辅助函数就不会将执行权转回给 person 的 greet 主方法,而是就此结束了。

也即是,结果将是:

(greet (make-instance 'person))
:around 
animal's greet :around 
NIL


终极测试,没有主方法的辅助方法

测试进行到最后,忽然一个狂想从我的脑子里崩出来:一个没有主方法的辅助方法,可能运行吗?

这就来试试:

(defclass person ()
    ()) 

(defmethod greet :around ((people person))
    (format t ":around ~%"))
 
测试:

 (greet (make-instance 'person))

*** - NO-PRIMARY-METHOD: When calling #<STANDARD-GENERIC-FUNCTION GREET> with
      arguments (#<PERSON #x2058E72E>), no primary method is applicable.
The following restarts are available:
RETRY          :R1      try calling GREET again
RETURN         :R2      specify return values
ABORT          :R3      Abort main loop


噢噢,解释器在抱怨我们没有可应用的主方法,因此也证明,没有主方法的辅助方法,是不行滴。


小结

嗯,关于辅助函数的测试就此做完了,下一篇文章,我将测试关于方法的继承的相关问题。

















 

 

 

 


 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值