【发布时间】:2016-08-18 09:48:32
【问题描述】:
假设我有如下代码设置
(defgeneric move (ship destination))
(defmethod move (ship destination)
;; do some fuel calculation here
)
(defmethod move :after ((ship ship) (dest station))
;; do things specific to landing on a station here
)
(defmethod move :after ((ship ship) (dest planet))
;; do things specific to landing on a planet here
)
现在假设我想将我的太空船移动到一个空间站,但燃料计算导致飞船上的燃料量为负数(即,行程不够用)。
我有没有办法防止:after 限定符被调用而不必发出错误信号?
如果我不停止通话,船将被移动到新位置而不会减去任何燃料,这实际上会破坏游戏。
【问题讨论】:
-
您可能应该将燃料计算移至
:AROUND方法并将两个:AFTER方法转换为主要方法。 -
@jkiiski 我如何阻止主要方法被调用?
-
你只是不叫它。
:AROUND方法必须手动使用CALL-NEXT-METHOD来调用主要方法,所以你可以有类似(when (sufficient-fuel) (call-next-method))的东西。 -
哦,好吧,但是不要在 main 方法的两边都调用 :around 吗?
-
主要方法是通过调用
CALL-NEXT-METHOD来调用的。
标签: common-lisp qualifiers multimethod