考虑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}>)为什么会发生这种情况?又是什么
被要求找到一个带有专门化器的方法。
这里不好吗?
在这里,主withdraw函数有两个参数acct和amt,因此为了从更具体的方法(使用3个参数而不是2个)调用它,我们可以向call-next-method提供不那么具体的withdraw方法的参数。但这还是没用的。
任何帮助都很感激。
发布于 2017-06-18 10:20:26
泛型函数的同义词列表()
泛型函数的方法需要有同余 lambda列表。语言标准描述了这意味着什么:同余Lambda-泛型函数的所有方法的列表。
正如你所看到的,第一条规则说:
必需的参数告诉我们必须提供哪些参数总是。泛型函数还允许可选、关键字和rest参数。但这是没有调度的。调度只适用于所需的参数和所有这些参数。
拥有相同数量的所需参数可以简化分派,并允许编译器使用错误的参数数检查函数调用。
可选参数需要是一致的,也是
还请注意,泛型函数的所有方法都需要有相同数量的可选参数。参见标准中的第二条规则。
措辞
示例:
(defun foo (a b) (list a b))a和b是函数foo的参数。
(foo (+ 2 3) (* 4 5))5和20是函数foo调用的两个参数。
https://stackoverflow.com/questions/44613571
复制相似问题