Object-Oriented Programming in FreeM

From FreeM Wiki
Revision as of 18:18, 29 November 2024 by Smw (talk | contribs)
Jump to navigation Jump to search

Classes

In FreeM, a class is defined by a routine. For instance, the STRING class (built into FreeM) is contained in the %STRING routine.

Example

%FRACTION(THIS,INIT):OBJECT ;
   S THIS("NUMERATOR"):PRIVATE=$P(INIT,"/",1)
   S THIS("DENOMINATOR"):PRIVATE=$P(INIT,"/",2)
   Q
GCD(THIS) Q $$GCDI^%FRACTION(THIS("NUMERATOR"),THIS("DENOMINATOR"))
DESTROY(THIS) ;
 Q
REDUCE(THIS) ;
 N G,NUMR,DENR
 S G=$$GCDI^%FRACTION(THIS("NUMERATOR"),THIS("DENOMINATOR"))
 S NUMR=THIS("NUMERATOR")/G
 S DENR=THIS("DENOMINATOR")/G
 Q NUMR_"/"_DENR
 ;
GCDI(A,B) Q:B=0 A S R=$$GCDI^%FRACTION(B,A#B) Q R


Constructors

A constructor must be the first entry point in a class definition, and the label name must match the routine name, and it must take two arguments, THIS and INIT. THIS represents the instance of the object being accessed, and INIT represents an initializer that can be used to assign an initial value to the object when instantiating the class.

On the first line of the above example, :OBJECT indicates that the FRACTION class inherits from OBJECT