We want to create a class file to be used as an Applet by a Browser. We need to extend the Java class java.applet.Applet and we start ad follows:
Java/* This is MyApplet.java */ import java.applet.Applet; public class MyApplet extends Applet { //... list of fields and methods } |
NetRexx/* This is MyApplet.nrx */ class MyApplet extends Applet --... list of properties and methods -- -- |
notes
| |
CLASS Instruction Sintax
class class_name [private|public|shared] [abstract|final|interface|adapter] [binary] visibility
private visible within the current package (default for all other classes in a file) shared visible within the current package (same as private) modifiers
abstract some method is abstract interface all methods are abstract and properties are constants adapter implements (automatically) all inherited abstract methods binary forces the use of native types extends indentifies the superclass uses lists classes sources of static properties and methods implements lists interfaces inherited |
Let's add a field to the Applet. In the init method we'll read the parameter MSG and then its value will be stored in a variable.
Java/* This is MyApplet.java */ import java.applet.Applet; public class MyApplet extends Applet { String message; //... list of methods } |
NetRexx/* This is MyApplet.nrx */ class MyApplet extends Applet message=String -- inheritable by default --... list of methods -- -- |
notes
| |
PROPERTIES Instruction Sintax properties [public|inheritable|private|shared] [constant|static|volatile|transient|indirect]; visibility
inheritable visible within the current package and to classes that extends the current class (default) shared visible within the current package but not inherited by classes that extends the current class private visible within the current class modifiers
static changeable property of the class volatile may change asynchronously transient not saved when the object is serialized indirect accessed through (automatically generated) get-set methods (JavaBeans convenction) |
Finally, we need the init() method (to initialize the message field reading the MSG parameter) and the paint(Graphics g) method (to show the message).
Java/* This is MyApplet.java */ import java.applet.Applet; public class MyApplet extends Applet { String message; public void init() { setBackground(Color.red); setForeground(Color.white); message = getParameter("MSG"); } public void paint(Graphics g) { g.drawString(message,10,10); } } |
NetRexx/* This is MyApplet.nrx */ class MyApplet extends Applet message=String method init setBackground(Color.red) setForeground(Color.white) message=getParameter("MSG") method paint(g=Graphics) g.drawString(message,10,10) -- -- -- -- |
| |
METHOD Instruction Sintax
method method_name[([argument_name[,...]])] visibility
inheritable visible within the current package and to classes that extends the current class (default) shared visible within the current package but not inherited by classes that extends the current class private visible within the current class modifiers
final cannot be overridden constant static and final abstract not implemented native implemented externally protect protects the current object (synchronization) returns defines the type returned (default NetRexx String) signal lists throwable exceptions |
Default values for the arguments of a method are an useful feature available in C++. Java doesn't possess it, but NetRexx does. Imagine a class that often needs to pass the same argument to one of its methods. With Java you can add another method with the same name that uses a default value and recalls the original one, otherwise you are obliged to pass that value every time you need it. In NetRexx it's sufficient to declare a value for that argument in the argument list. For example, consider a Sphere class that must be scaled (often by a 2 factor):
Java/* This is Sphere.java */ public class Sphere { private float radius = 1.0; Sphere(float radius) { this.radius = radius; } public void Scale(float factor) { radius *= factor; } public void Scale() { Scale(2.0); } } // In another class Sphere sph = new Sphere(); sph.Scale(); // doubles the radius sph.Scale(0.5); // halves the radius |
NetRexx/* This is Sphere.nrx */ class Sphere properties private radius method Sphere(radius=1.0) this.radius = radius method Scale(factor=2.0) radius = radius * factor -- -- -- -- -- -- In another class sph=Sphere sph.Scale -- doubles the radius sph.Scale(0.5) -- halves the radius |
|
A method returns values with the return keyword. For example, let's change the Sphere class in a more configurable one:
Java/* This is Sphere.java */ public class Sphere { private float radius = 1.0; public Sphere(float r) { radius = r; } public float getRadius() { return radius; } public void setRadius(float r) { radius = r; } } |
NetRexx/* This is Sphere.nrx */ class Sphere properties private radius method Sphere(r=1.0) radius=r method getRadius return radius method setRadius(r) radius = r -- -- -- |
NetRexx (experimental)/* This is Sphere.nrx */ class Sphere properties indirect radius method Sphere(r=1.0) radius=r -- -- -- -- -- -- -- |
|
Objects are created using special methods called constructors. Constructors, as in Java, have the same name of the class. Instances of classes (objects) are created without the new keyword and, as in normal methods, arguments may have default values.
Java/* Examples of new Java objects */ Vector v1 = new Vector(); // empty Vector Vector v2 = new Vector(10); // with capacity |
NetRexx/* Examples of new NetRexx objects */ v1=Vector -- an empty Vector v2=Vector(10) -- with capacity |
|
[Index] [Previous Chapter] [Next Chapter]