English 中文(简体)
Rexx - Object Oriented
  • 时间:2024-12-22

Rexx - Object Oriented


Previous Page Next Page  

When you install ooRexx as per the environment chapter, you will also have the abipty to work with classes and objects. Please note that all of the following code needs to be run in the ooRexx interpreter. The normal Rexx interpreter will not be able to run this object oriented code.

Class and Method Declarations

A class is defined with the following Syntax declaration.

Syntax

::class classname 

where classname is the name given to the class.

A method in a class is defined with the following Syntax declaration.

Syntax

::method methodname 

Where methodname is the name given to the method.

A property in a class is defined with the below Syntax declaration.

Syntax

::attribute propertyname 

Where propertyname is the name given to the property.

Example

The following is an example of a class in Rexx.

::class student 
::attribute StudentID 
::attribute StudentName 

The following points need to be noted about the above program.

    The name of the class is student.

    The class has 2 properties, StudentID and StudentName.

Getter and Setter Methods

The Getter and Setter methods are used to automatically set and get the values of the properties. In Rexx, when you declare a property with the attribute keyword, the getter and setter methods are already put in place.

Example

::class student 
::attribute StudentID 
::attribute StudentName 

In the above example, there would be Getter and Setter methods for StudentId and StudentName.

An example of how they can be used is shown in the following program.

/* Main program */ 
value = .student~new 
value~StudentID = 1 
value~StudentName =  Joe  
say value~StudentID 
say value~StudentName 

exit 0 
::class student 
::attribute StudentID 
::attribute StudentName 

The output of the above program will be as shown below.

1 
Joe 

Instance Methods

Objects can be created from the class via the ~new operator. A method from the class can be called in the following way.

Object~methodname 

Where methodname is the method which needs to be invoked from the class.

Example

The following example shows how an object can be created from a class and the respective method invoked.

/* Main program */ 
value = .student~new 
value~StudentID = 1 
value~StudentName =  Joe  
value~Marks1 = 10 
value~Marks2 = 20 
value~Marks3 = 30 
total = value~Total(value~Marks1,value~Marks2,value~Marks3) 
say total 

exit 0 
::class student 
::attribute StudentID 
::attribute StudentName 
::attribute Marks1 
::attribute Marks2 
::attribute Marks3 
::method  Total  
use arg a,b,c 
return (ABS(a) + ABS(b) + ABS(c)) 

The output of the above program will be as shown below.

60 

Creating Multiple Objects

One can also create multiple objects of a class. The following example will show how this can be achieved.

In here we are creating 3 objects (st, st1 and st2) and calpng their instance members and instance methods accordingly.

Let’s take a look at an example of how multiple objects can be created.

Example

/* Main program */ 
st = .student~new 
st~StudentID = 1 
st~StudentName =  Joe  
st~Marks1 = 10 
st~Marks2 = 20 
st~Marks3 = 30 
total = st~Total(st~Marks1,st~Marks2,st~Marks3) 
say total  

st1  =  .student~new 
st1~StudentID = 2 
st1~StudentName =  John  
st1~Marks1 = 10 
st1~Marks2 = 20 
st1~Marks3 = 40 
total = st1~Total(st1~Marks1,st1~Marks2,st1~Marks3) 
say total  

st2  =  .student~new 
st2~StudentID = 3 
st2~StudentName =  Mark  
st2~Marks1 = 10 
st2~Marks2 = 20 
st2~Marks3 = 30 
total = st2~Total(st2~Marks1,st2~Marks2,st2~Marks3) 
say total  

exit 0 
::class student 
::attribute StudentID 
::attribute StudentName 
::attribute Marks1 
::attribute Marks2 
::attribute Marks3 
::method  Total  
use arg a,b,c 
return (ABS(a) + ABS(b) + ABS(c)) 

The output of the above program will be as shown below.

60 
70 
60 

Inheritance

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance, the information is made manageable in a hierarchical order.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Let’s see an example of inheritance in Rexx. In the following example we are creating a class called Person. From there we use the subclass keyword to create the Student class as a sub-class of Person.

Example

/* Main program */ 
st = .student~new 
st~StudentID = 1 
st~StudentName =  Joe  
st~Marks1 = 10 
st~Marks2 = 20 
st~Marks3 = 30 
say st~Total(st~Marks1,st~Marks2,st~Marks3)  

exit 0 
::class Person 
::class student subclass Person 
::attribute StudentID 
::attribute StudentName 
::attribute Marks1 
::attribute Marks2 
::attribute Marks3 
::method  Total  
use arg a,b,c 
return (ABS(a) + ABS(b) + ABS(c)) 

The output of the above program will be as shown below.

60
Advertisements