Difference between revisions of "Object-Oriented Programming in FreeM"

From FreeM Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
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 ==
+
 
 +
== 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.
 +
 
 +
A constructor looks like this:
  
 
<pre>
 
<pre>
Line 10: Line 14:
 
   S THIS("DENOMINATOR"):PRIVATE=$P(INIT,"/",2)
 
   S THIS("DENOMINATOR"):PRIVATE=$P(INIT,"/",2)
 
   Q
 
   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>
 
</pre>
  
 +
The general syntax of a constructor is <code>&lt;routine-name&gt;(THIS,INIT)[:&lt;superclass&gt;]</code>, where <code>superclass</code> represents the name of a class from which this class should inherit. In the above example, the <code>FRACTION</code> class inherits from the <code>OBJECT</code> class. Note that this is not strictly necessary in this case, as all classes automatically inherit from <code>OBJECT</code>.
 +
 +
== Destructors ==
 +
A destructor is called when you <code>KILL</code> an instance variable. It must be named <code>DESTROY</code>, and must take one argument (<code>THIS</code>). The destructor should be used to clean up any resources used by class methods.
 +
 +
== Inheritance Rules ==
 +
Every class you create will automatically inherit the methods and functionality of the <code>OBJECT</code> class, supplied with FreeM.
 +
 +
When attempting to call a method, FreeM will first search the class routine for a matching entry point, and then follow the inheritance chain upwards until a matching entry point is found. If the final class in the chain does not have a matching entry point, FreeM will try to find a matching entry point in the <code>OBJECT</code> class.
  
== Constructors ==
+
== Instantiating Classes ==
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.
+
To instantiate a class (i.e., create an object from a certain class), you will use the <code>NEW</code> command as follows:
 +
 
 +
<pre>
 +
NEW MYSTR=$#^%STRING("myString")
 +
</pre>
  
On the first line of the above example, <code>:OBJECT</code> indicates that the <code>FRACTION</code> class inherits from <code>OBJECT</code>
+
This will create a local variable called <code>MYSTR</code> of type <code>STRING</code>, and initialize it with the value <code>myString</code>.

Revision as of 18:29, 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.


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.

A constructor looks like this:

%FRACTION(THIS,INIT):OBJECT ;
   S THIS("NUMERATOR"):PRIVATE=$P(INIT,"/",1)
   S THIS("DENOMINATOR"):PRIVATE=$P(INIT,"/",2)
   Q

The general syntax of a constructor is <routine-name>(THIS,INIT)[:<superclass>], where superclass represents the name of a class from which this class should inherit. In the above example, the FRACTION class inherits from the OBJECT class. Note that this is not strictly necessary in this case, as all classes automatically inherit from OBJECT.

Destructors

A destructor is called when you KILL an instance variable. It must be named DESTROY, and must take one argument (THIS). The destructor should be used to clean up any resources used by class methods.

Inheritance Rules

Every class you create will automatically inherit the methods and functionality of the OBJECT class, supplied with FreeM.

When attempting to call a method, FreeM will first search the class routine for a matching entry point, and then follow the inheritance chain upwards until a matching entry point is found. If the final class in the chain does not have a matching entry point, FreeM will try to find a matching entry point in the OBJECT class.

Instantiating Classes

To instantiate a class (i.e., create an object from a certain class), you will use the NEW command as follows:

NEW MYSTR=$#^%STRING("myString")

This will create a local variable called MYSTR of type STRING, and initialize it with the value myString.