Call an object method: Difference between revisions
Content added Content deleted
No edit summary |
(Added Common Lisp) |
||
Line 190: | Line 190: | ||
foo.instanceMethod() #=> 'Baz' |
foo.instanceMethod() #=> 'Baz' |
||
Foo.staticMethod() #=> 'Bar'</lang> |
Foo.staticMethod() #=> 'Bar'</lang> |
||
=={{header|Common Lisp}}== |
|||
In Common Lisp, classmethods are methods that apply to classes, rather than classes that contain methods. |
|||
<lang lisp>(defclass my-class () |
|||
((x |
|||
:accessor get-x ;; getter function |
|||
:initarg :x ;; arg name |
|||
:initform 0))) ;; initial value |
|||
;; declaring a public class method |
|||
(defmethod square-x ((class-instance my-class)) |
|||
(* (get-x class-instance) (get-x class-instance))) |
|||
;; create an instance of my-class |
|||
(defvar *instance* |
|||
(make-instance 'my-class :x 10)) |
|||
(format t "Value of x: ~a~%" (get-x *instance*)) |
|||
(format t "Value of x^2: ~a~%" (square-x *instance*)) |
|||
</lang> |
|||
Output (CLISP v2.49): |
|||
<pre>$ clisp object.cl |
|||
Value of x: 10 |
|||
Value of x^2: 100 |
|||
</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |