Another Sample Program

We mentioned that a directive is non-executable code that begins with a double-colon (::) and follows all the other program code. The ::CLASS directive creates a class; in this sample, the Dinosaur class. The sample provides two methods for the Dinosaur class, INIT and DIET. These are added to the Dinosaur class using ::METHOD directives. After the line containing the ::METHOD directive, the code for the method is specified. Methods are ended either by the start of the next directive or by the end of the program.

Since directives must follow the executable code in your program, you put that code first. In this case, the executable code creates a new dinosaur, Dino, that is an instance of the Dinosaur class. REXX then runs the INIT method (REXX runs any INIT method automatically whenever the NEW message is received). Here the INIT method is used to identify the type of dinosaur. Next the program runs the DIET method to determine whether the dinosaur eats meat or veggies. The information returned by INIT and DIET is saved by REXX as variables in the Dino object.

In the example, the Dinosaur class and its two methods are defined following the executable program code:

dino=.dinosaur~new         /* Create a new dinosaur instance and initialize variables */
dino~diet                  /* Run the DIET method                                     */
exit
::class Dinosaur           /* Create the Dinosaur class  */

  ::method init            /* Create the INIT method     */
    expose type
    say "Enter a type of dinosaur."
    pull type
    return

  ::method diet            /* Create the DIET method     */
    expose type
    select
    when type="T-REX" then string="Meat-eater"
    when type="TYRANNOSAUR" then string="Meat-eater"
    when type="TYRANNOSAURUS REX" then string="Meat-eater"
    when type="DILOPHOSAUR" then string="Meat-eater"
    when type="VELICORAPTOR" then string="Meat-eater"
    when type="RAPTOR" then string="Meat-eater"
    when type="ALLOSAUR" then string="Meat-eater"
    when type="BRONTOSAUR" then string="Plant-eater"
    when type="BRACHIOSAUR" then string="Plant-eater"
    when type="STEGOSAUR" then string="Plant-eater"
    otherwise string="Type of dinosaur or diet unknown"
    end
    say string
    return 0


[Back: A Sample Program Using Directives]
[Next: Creating Classes Using Messages]