Another Sample Program

A directive is nonexecutable code that begins with a double colon (::) and follows the program code. The ::CLASS directive creates a class; in this example, the Dinosaur class. The sample provides two methods for the Dinosaur class, INIT and DIET. These are added to the Dinosaur class using the ::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.

Because 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. Then the program runs the DIET method to determine whether the dinosaur eats meat or vegetables. Rexx saves the information returned by INIT and DIET as variables in the Dino object.

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


Defining methods
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