Things that aren't strings


In all the examples so far, the data being manipulated (numbers, words, and so on) are expressed as a string of characters. Many things, however, can be expressed more easily in some other way, so NetRexx allows variables to refer to other collections of data, which are known as objects.

Objects are defined by a name that lets NetRexx determine the data and methods that are associated with the object. This name identifies the type of the object, and is usually called the class of the object.

For example, an object of class Oblong might represent an oblong to be manipulated and displayed. The oblong could be defined by two values: its width and its height. These values are called the properties of the Oblong class.

Most methods associated with an object perform operations on the object; for example a 'size' method might be provided to change the size of an Oblong object. Other methods are used to construct objects (just as for NetRexx arrays, an object must be constructed before it can be used). In NetRexx and Java, these constructor methods always have the same name as the class of object that they build (Oblong, in this case).

Here's how an Oblong class might be written in NetRexx (by convention, this would be written in a file called Oblong.nrx; Java expects the name of the file to match the name of the class inside it):

  /* Oblong.nrx -- simple oblong class */
  class Oblong
    width       -- size (X dimension)
    height      -- size (Y dimension)

/* Constructor method to make a new oblong */ method Oblong(new_width, new_height) -- when we get here, a new (uninitialized) object has been -- created. Copy the parameters we have been given to the -- properties of the object: width=new_width; height=new_height

/* Change the size of an Oblong */ method size(new_width, new_height) returns Oblong width=new_width; height=new_height return this -- return the resized object

/* Change the size of an Oblong, relative to its current size */ method sizerelative(rel_width, rel_height) returns Oblong width=width+rel_width; height=height+rel_height return this

/* 'Print' what we know about the oblong */ method print say 'Oblong' width 'x' height

To summarize:

  1. A class is started by the 'class' statement, which names the class.
  2. The class statement is followed by a list of the properties of the object. These can be assigned initial values, if required.
  3. The properties are followed by the methods of the object. Each method is introduced by a method statement which names the method and describes the arguments that must be supplied to the method. The body of the method is ended by the next method statement (or by the end of the file).

The Oblong.nrx file is compiled just like any other NetRexx program, and should create a class file called Oblong.class. Here's a program to try out the Oblong class:

  /* tryOblong.nrx -- try the Oblong class */

first=Oblong(5,3) -- make an oblong first.print -- show it first.sizerelative(1,1).print -- enlarge it and print it again

second=Oblong(1,2) -- make another oblong second.print -- and print it

when 'tryOblong.nrx' is compiled, you'll notice that the cross-reference listing of variables shows that the variables 'first' and 'second' have type 'Oblong'. These variables refer to Oblongs, just as the variables in earlier examples referred to Rexx strings.

Once a variable has been assigned a type, it can only refer to objects of that type. This helps avoid errors where a variable refers to an object that it wasn't meant to.

Programs are classes, too

It's worth pointing out, here, that all the example programs in this document are in fact classes (you may have noticed that compiling them creates xxx.class files, where xxx is the name of the source file). The Java environment will allow a class to run as a stand-alone application if it has a constant method called 'main' which takes an array of Java Strings as its argument.

If necessary (that is, if there is no class statement) NetRexx automatically adds the necessary class and method statement, and also a statement to convert the array of strings (each of which holds one word from the command string) to a single Rexx string. The 'toast' example could therefore have been written:

  /* This wishes you the best of health. */
  class toast
    method main(argwords=String[]) constant; arg=Rexx(argwords)
      say 'Cheers!'

[ previous section | contents | next section ]

From 'nrover.doc', version 1.113.
Copyright(c) IBM Corporation, 1996, 1997. All rights reserved. ©