methodmethod — defines a method for a given class.
method class.method_name ([argument [, argument]...]) statement
classThe class for which this method is defined.
method_nameThe name of the method.
argumentsThe argument list for this method. This does not include the implied argument self, nor is self defined when the arguments are bound as the method is called.
statementThe body code statment for this method. Within this statement
the special variable self is defined as the
instance on which this method is being applied.
A function definition of the resulting method function.
This statement defines a method function for a given class. If a method already exists for this class with this name, the previous definition will be replaced. If a method of the same name exists for any parent (base) class of the given class, it will be overridden for instances of this class only. In Gamma methods are run using the syntax:
instance.method_name(arguments);
which is the familiar object.method syntax
used in C++.
![]() | |
|
Gamma>class RegPolygon{sides; length;}(defclass RegPolygon nil [][length sides])Gamma>method RegPolygon.perimeter (){.sides * .length;}(defun RegPolygon.perimeter (self) (* (@ self sides) (@ self length)))Gamma>class Square RegPolygon {sides = 4;}(defclass Square RegPolygon [][length (sides . 4)])Gamma>method Square.area (){sqr(self.length);}(defun Square.area (self) (sqr (@ self length)))Gamma>sqB = new(Square);{Square (length) (sides . 4)}Gamma>sqB.length = 3;3Gamma>sqB.perimeter();12Gamma>sqB.area();9Gamma>