class

class — defines a class.

Syntax

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

[static:class_var[= initial_value];]
...

}

		

Arguments

class_name

The name of the new class.

parent

The parent (base) class.

class_var

Class variable definition.

instance_var

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

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

Returns

A class definition.

Description

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.

[Note]

    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.

Example

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])
Gamma> 
		

See Also

Class Operators, class_add_cvar, class_add_ivar, method, new