(define-class <my-class> (<super-class>) (field1 init-value: 1) ... (fieldn init-value: 3) )declares
<my-class>
to be a subclass of <super-class>
. The init-value keyword specifies the default value of a field when creating a class instance. To create an instance use the function make. For example,
(set! mo (make <my-class> field1: 7)) (print mo) (instance? mo <my-class>)sets mo to be an instance of
<my-class>
whose value for field1 is 7. The Rschme function instance? checks whether an object is an instance of a given class.
Methods on a class are defined using the constructor define-method
. For example,
(define-method plus ((self <my-class>) (x <number>)) (+ x (field1 self)))defines the method
plus
on the class <my-class>
. The call (plus mo 5) will produce 12 as an answer. The constructor next-method
is used to invoke the same method of the super class. For example,
(define-class <my-class-1> (<my-class>)) (define-method plus ((self <my-class-1>) (x <number>)) (+ x (next-method self x)))The class system of Rscheme does not enforce public or private fields as some other systems do. All fields of an object are public. In addition, Rscheme does not allow multiple definitions of the same method for a given class. For example,
(define-method plus ((self <my-class>) (y <my-class>)) (+ (field1 self) (field1 y)))overwrites the previous definition of the method plus on the class
<
my-class>
.