考慮類account
:呼叫下一個最具體的方法是行不通的
(defclass account()
((name :initarg :name :reader name)
(balance :initarg :balance :initform 0.00 :accessor balance)
(interest-rate :allocation :class :initform 0.06
:reader interest-rate)))
對於這個類,我們定義了一個方法withdraw
:
(defmethod withdraw ((acct account) amt)
(if (< amt (balance acct))
(decf (balance acct) amt)
'insufficient-funds))
而且,另一個類password-account
,即account
的子類:
(defclass password-account (account)
((password :initarg :password :reader password)))
而且,該方法withdraw
,該類:
(defmethod withdraw ((acct password-account) amt pass)
(if (equal (password acct) pass)
(call-next-method acct amt)
'wrong-password))
但是,這給出了一個錯誤:
The generic function
#<STANDARD-GENERIC-FUNCTION COMMON-LISP-USER::WITHDRAW (1)>
takes 2 required arguments; was asked to find a method with
specializers
(#<STANDARD-CLASS COMMON-LISP-USER::PASSWORD-ACCOUNT>
#1=#<SB-PCL:SYSTEM-CLASS COMMON-LISP:T> #1#)
[Condition of type SB-PCL::FIND-METHOD-LENGTH-MISMATCH]
See also:
Common Lisp Hyperspec, FIND-METHOD [:function]
Restarts:
0: [RETRY] Retry SLIME REPL evaluation request.
1: [*ABORT] Return to SLIME's top level.
2: [ABORT] abort thread (#<THREAD "repl-thread" RUNNING {1005308033}>)
這究竟是爲什麼?又是什麼
被要求找到specializers的方法
這裏的意思?
這裏,主withdraw
函數有兩個參數acct
和amt
,所以爲了從一個更具體的方法,它使用的2 3個參數,而不是調用它,我們可以提供call-next-method
與較不具體withdraw
方法的參數。但是這仍然不起作用。
任何幫助表示讚賞。
在同一個泛型函數中,不能有兩個必需參數和另一個需要三個參數的方法。 CLOS不允許這樣做。第一個方法定義確定所需參數的數量。在你的例子中的泛型函數有兩個必需的參數,你試圖用三個必需的參數來定義一個方法。這不被支持,也不被允許。 –
謝謝@RainerJoswig。不支持這些方法的任何特定原因? – Mooncrater
通過帶參數的'call-next-method'調用下一個最具體的方法有什麼意義?因爲所有的方法都需要相同數量的參數。 (因此相同的參數) – Mooncrater