It's common, when dealing with objects, to take an existing class and extend it. One way to do this is to modify the source code of the original class -- but this isn't always available, and with many different people modifying a class, classes could rapidly get over-complicated.
Languages that deal with objects, like NetRexx, therefore allow new classes of objects to be set up which are derived from existing classes. For example, if you wanted a different kind of Oblong in which the Oblong had a new property that would be used when printing the Oblong as a rectangle, you might define it thus:
/* charOblong.nrx -- an oblong class with character */ class charOblong extends Oblong printchar -- the character for display/* Constructor method to make a new oblong with character */ method charOblong(new_width, new_height, new_printchar) super(new_width, new_height) -- make an oblong printchar=new_printchar -- and set the print character
/* 'Print' the oblong */ method print loop for this.height say printchar.copies(this.width) end
There are several things worth noting about this example:
Another common way of saying this is that 'charOblong' is a subclass of 'Oblong' (and 'Oblong' is the superclass of 'charOblong').
The charOblong.nrx file is compiled just like Oblong.nrx was, and should create a file called charOblong.class. Here's a program to try it out:
/* trycharOblong.nrx -- try the charOblong class */first=charOblong(5,3,'#') -- make an oblong first.print -- show it first.sizerelative(1,1).print -- enlarge it and print it again
second=charOblong(1,2,'*') -- make another oblong second.print -- and print it
This should create the two charOblong objects, and print them out in a simple 'character graphics' form. Note the use of the method 'sizerelative' from Oblong to resize the charOblong object.
All methods in NetRexx may have optional arguments (omitted from the right) if desired. For an argument to be optional, you must supply a default value. For example, if the charOblong constructor was to have a default printchar value, its method statement could have been written:
method charOblong(new_width, new_height, new_printchar='X')
which indicates that if no third argument is supplied then 'X' should be used. A program creating a charOblong could then simply write:
first=charOblong(5,3) -- make an oblong
which would have the same effect as if 'X' were specified as the third argument.
[ previous section | contents | next section ]
From 'nrover.doc', version 1.113.
Copyright(c) IBM Corporation, 1996, 1997. All rights reserved. ©