class — defines a class.


class class_name [parent]
[instance_var [= initial_value];]

[static:class_var[= initial_value];]





The name of the new class.


The parent (base) class.


Class variable definition.


Instance variable definition. This is provided in the form of a list of variable definitions. Each variable definition is either a variable name or a list which contains a variable name and a default value expression. Whenever a new instance is formed, the default value expression is evaluated to the default value. If no default value is given, the instance variable's value will be nil.


Initial value given to instance_var, if none then nil is assigned to that instance variable.


A class definition.


This function constructs a class definition and binds the class-name symbol in the current scope to refer to that class. The class mechanism allows only a single parent (base) class. None of the arguments to class is evaluated. If instance_vars are defined with the same names as inherited variables, the inherited variables are overridden and cannot be accessed by instances of this class.


    The class statement creates a new class.

    If the parent (base) class is omitted, or is nil, then the resulting class has no parent (base).

    Each instance variable consists of a name (a symbol) and an optional initial value that will be assigned whenever a new instance of the class is created using the new function.

    The resulting class definition, which is a data object in its own right, will be assigned to the symbol, name.


This example creates two classes: a base class, RegPolygon; and a class derived from it, Square. RegPolygon has two attributes: sides and length. When Square is created, its parent (base) class (RegPolygon) is explicitly assigned. In addition, the attibute sides is assigned a value of 4.

Gamma> class RegPolygon{sides; length;}
(defclass RegPolygon nil [][length sides])
Gamma> class Square RegPolygon {sides = 4;}
(defclass Square RegPolygon [][length (sides . 4)])

This example creates a class with instance variables and class variables.

Gamma> class Other {ivar1; ivar2; static: cvar1; cvar2;}
(defclass Other nil [cvar1 cvar2][ivar1 ivar2])

See Also

Class Operators, class_add_cvar, class_add_ivar, method, new