Tob*_*ndt 4 lisp common-lisp clos
您如何在Lisp中表达以下Java代码?
class Foo {
private String s;
public Foo(String s) {
this.s = s;
}
}
class Bar extends Foo {
public Bar(int i) {
super(Integer.toString(i));
}
}
Run Code Online (Sandbox Code Playgroud)
在Lisp中,是make-instance
或者initialize-instance
相当于构造函数?如果是,你如何调用超类构造函数?
Rai*_*wig 14
使用CALL-NEXT-METHOD
从方法中调用下一个.
通常只使用标准的方法组合设施和写:BEFORE
,:AFTER
或:AROUND
用于方法INITIALIZE-INSTANCE
.
单程:
(defclass foo ()
((s :type string :initarg :s)))
(defclass bar (foo) ())
(defmethod initialize-instance :around ((b bar) &key i)
(call-next-method b :s (princ-to-string i)))
Run Code Online (Sandbox Code Playgroud)
例:
CL-USER 17 > (make-instance 'bar :i 10)
#<BAR 402000B95B>
CL-USER 18 > (describe *)
#<BAR 4130439703> is a BAR
S "10"
Run Code Online (Sandbox Code Playgroud)
请注意,我调用时CALL-NEXT-METHOD
没有更改它调度的参数.我刚刚更改了&key
initarg参数.