Difference between revisions of "Object-Oriented Programming in FreeM"
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
In FreeM, a class is defined by a routine. For instance, the <code>STRING</code> class (built into FreeM) is contained in the <code>%STRING</code> routine. | In FreeM, a class is defined by a routine. For instance, the <code>STRING</code> class (built into FreeM) is contained in the <code>%STRING</code> routine. | ||
+ | |||
+ | == Example == | ||
+ | |||
+ | <pre> | ||
+ | %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 | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | == 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, <code>THIS</code> and <code>INIT</code>. <code>THIS</code> represents the instance of the object being accessed, and <code>INIT</code> 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, <code>:OBJECT</code> indicates that the <code>FRACTION</code> class inherits from <code>OBJECT</code> |
Revision as of 18:18, 29 November 2024
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