我試圖找到Common Lisp的CLOS解決典型的菱形繼承問題。代碼:鑽石繼承和Common Lisp的對象系統
(defclass C1.0() ...)
(defclass C2.1 (C1.0) ...)
(defclass C2.2 (C1.0) ...)
(defclass C3.0 (C2.1 C2.2) ...)
(defmethod m1 ((obj C1.0)) ...)
(defmethod m1 ((obj C2.1)) ...)
(defmethod m1 ((obj C2.2)) ...)
(defmethod m1 ((obj C3.0))
; Here I want to call the C2.2 version of
; m1
...)
此外,假定C1.0,C2.1和C2.2的代碼中,我沒有使用圖書館,所以我不能有任何修改。 Furthur,假設一些其他類也將從C2.2派生和可能不想打電話M1的C2.2版本,因此使用:before
我真的不能添加任何C2.2。使用call-next-method
將調用C2.1版本。
只是爲了澄清,這是怎麼解決它在Python:
class C1_0 :
def m1 (self) : ...
class C2_1 (C1_0) :
def m1 (self) : ...
class C2_2 (C1_0) :
def m1 (self) : ...
class C3_0 (C2_1, C2_2) :
def m1 (self) :
C2_2.m1 (self) # <-- This is the solution
...
非常感謝您的澄清。我仍然在學習LISP,並且來自Python和C++等語言的背景,有時我傾向於忘記CLOS與「常見」面向對象系統的區別,我嘗試儘量減少,但不夠成功:) – RKS 2014-11-22 18:07:54