Here is a program that uses directives to create new classes and methods:
|asav = .savings~new /* executable code begins */|
|say asav~type /* executable code */|
|asav~name= "John Smith" /* executable code ends */|
|::class Account /* directives begin ... */|
|return "an account"|
|::class Savings subclass Account|
|return "a savings account" /* ... directives end */|
The preceding program uses the ::CLASS directive to create two classes, the Account class and its Savings subclass. In the ::class Account expression, the ::CLASS directive precedes the name of the new class, Account.
The example program also uses the ::METHOD directive to create a TYPE method and ::ATTRIBUTE to create NAME and NAME= methods for Account. In the ::method type instruction, the ::METHOD directive precedes the method name, and is immediately followed by the code for the method. Methods for any new class follow its ::CLASS directive in the program, and precede the next ::CLASS directive.
In the ::attribute name directive, we're creating a pair of methods. The NAME ("getter") method returns the current value of the NAME object variable. The NAME= ("setter") method can assign a new value to the NAME object variable.
You do not have to associate object variables with a specific object. Rexx keeps track of object variables for you. Whenever you send a message to savings account asav, which points to the Name object, Rexx knows what internal object value to use. If you assign another value to asav (such as "Mary Smith"), Rexx recovers the object that was associated with asav ("John Smith") as part of its normal garbage-collection operations.
In the Savings subclass, a second TYPE method is created that supersedes the TYPE method Savings would otherwise have inherited from Account. Note that the directives appear after the program code.