Difference between revisions of "Recital Object-Oriented Programming"
Barrymavin (Talk | contribs) (→Special variables) |
Barrymavin (Talk | contribs) (→Special variables) |
||
Line 274: | Line 274: | ||
There are several special built-in object variables that can be used. | There are several special built-in object variables that can be used. | ||
− | {| | + | {| class="wikitable" |
!Object!!Description | !Object!!Description | ||
|- | |- |
Revision as of 01:40, 24 October 2009
Contents
- 1 Recital Object-Oriented Programming
- 1.1 Classes and Objects
- 1.2 Class members
- 1.3 Class inheritance and subclassing
- 1.4 Overriding methods
- 1.5 Scope resolution operator
- 1.6 Special variables
- 1.7 Iterating through object properties
- 1.8 Dynamically adding and removing properties runtime
- 1.9 Dynamically assigning methods at runtime
- 1.10 Runtime data-type checking
- 1.11 Understanding member lookup
- 1.12 The life of an object
- 1.13 Writing and using class libraries
Recital Object-Oriented Programming
Recital is a dynamic programming language particularly suited to the development of database applications. While Recital still supports standard procedural programming, new extensions to the language give you the power and flexibility of object-oriented programming. Object-oriented design and object-oriented programming represent a change in focus from standard procedural programming. This short primer will give you a good understanding of how to program object-oriented Recital.
Classes and Objects
Classes and objects are closely related, but they are not the same. A class contains information about how an object should look and behave. A class is the blueprint or template for an object.
All of the properties and methods for an object are specified in the class definition. In addition, classes have the following characteristics that make them especially useful for creating reusable, easily maintained code:
- Encapsulation
- Subclasses
- Inheritance
- Interfaces
To define a class use the following syntax.
class myclass // class members endclass
Then, to create an object based on this class use the following syntax.
myobject = new myclass()
Class members
Class members consists of properties (variables) and methods (procedures) that are encapsulated within the class declaration.
Properties
An object has certain properties, or attributes that are specific to the object. Properties are the equivalent of variables that can only be accessed by referencing them inside an object. By encapsulating the data (variables) and procedures within a class, programming becomes less error prone and applications easier to maintain.
Objects you create in Recital have properties that are determined by the class the object is based on. As Recital is a dynamic language, classes in Recital allow properties and methods to be added at run time.
class myclass myprop = “hello world” endclass myobject = new myclass() echo myobject.myprop // displays "hello world"
Properties defined within classes can be simple variables, fixed arrays, dynamic arrays or objects.
Methods
Methods are procedures that are associated with objects. Methods are different from normal Recital procedures in that they are bound with an object and are called differently from the way normal Recital procedures are called.
class myclass myprop = “Hello world” public procedure mymethod() echo myprop endproc endclass myobject = new myclass() myobject.mymethod() // displays "hello world"
Interface Methods
Interface Methods are template procedure definitions that must be implemented in any derived classes.
class myclass myprop = “Hello world” public procedure mymethod() interface endclass myobject = new myclass() myobject.mymethod() // throws an error "Interface method not defined"
Base Methods
All classes created inherit from the object class. The object class has a set of built-in methods that are available to all classes.
Method | Description |
---|---|
addproperty | Dynamically add a property to an object.
e.g. |
removeproperty | Dynamically remove a property from an object. e.g. |
clone | Clone this object. e.g. |
tostring | Returns a string describing the object
e.g. |
class | Returns the classname of this object</td> |
instanceof | Returns True if this object is an instance of the specified class
e.g. |
equals | Compares the contents of two objects and returns True if they contain the same property values. If properties are arrays or other objects then the comparison is done recursively.
e.g. |
save | Saves an object to an external file (default extension .obf)
e.g. |
load | Loads an object from an external file (default extension .obf)
e.g. |
Member access control modifiers
You can 'hide' properties and methods using the access control modifiers protected, hidden, public, private, or static.
class myclass private myprop = “Hello world” public procedure mymethod() return myprop endproc public static procedure mystaticmethod() endproc endclass myobject = new myclass() // throws an error as "myprop" is private and can only be accessed by methods inside the class echo myobject.myprop myobject = new myclass() // this will work. displays "hello world" myobject.mymethod()
Special methods
Several methods hold special significance in a class.
Name | Description |
---|---|
init | Called after an object is first created. This is a known as the constructor method. |
Destroy | Called just prior to an object being destroyed. This is known as the destructor method. |
property_access | Property access notification. |
property_assign | Property assignment notification. |
this_access | Property access notification. |
this_assign | Property assignment notification. |
error | Class specific error handler. |
Constructors and Destructors
Recital allows developers to declare constructor and destructor methods for classes. Classes which have a constructor method called init call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
class myclass procedure init() endproc endclass obj = new myclass() // the init() method is called with no arguments
You can pass arguments to the constructor method when the object is created.
class myclass public name public title procedure init(pname, ptitle) name = pname title = ptitle endproc endclass obj = new myclass("joe", "developer") // the init() method is called with arguments
Similarly, Recital uses a destructor concept similar to that of other object-oriented languages, such as Java. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. The Recital destuctor method is called destroy.
class myclass procedure destroy() endproc endclass obj = new myclass() obj = null // the destroy() method will be called
Class inheritance and subclassing
There are many benefits of inheritance with Recital, the most common is simplifying and reducing instances of redundant code. Recital supports multiple levels of inheritance. A class that inherits a parent class is said to be a 'subclass' of the parent.
class product public name public price proc init(cName, nPrice) // contructor name = cName price = nPrice endproc proc display() echo "Product name is " + name + ", price is " + price endproc endclass class food as product private weight endclass class car as product private color endclass class motorbike as product private size endclass // now define a vehicle class that inherits from multiple classes (car and motorbike) class vehicle as car,motorbike public manufacturer = "Ford" public yearDesigned proc init(cName, nPrice) // contructor manufacturer = cName price = nPrice endproc proc display() echo "Manufacturer is " + manufacturer + ", price is " + price endproc endclass // create a new object myobject = new vehicle("Ford", 20000) myobject.display() // displays "Manufacturer is Ford, price is 20000 myobject = new motorbike("Honda", 1500) myobject.display() // displays "Product name is Honda, price is 20000
Overriding methods
As can be seen by the above example of class inheritance, you can override methods of parent classes that you inherit. If you want to call the parent class method of the same name from within a subclass, then use the dodefault() method. Note that because Recital handles multiple inheritance, and because a subclass can only have one 'parent' the last class inherited is denoted as the parent and is called by dodefault().
// now define a vehicle class that inherits from multiple classes (car and motorbike) class vehicle as car,motorbike public manufacturer = "Ford" public yearDesigned proc init(cName, nPrice) // contructor manufacturer = cName name = cName price = nPrice endproc proc display() dodefault() // calls the 'display()' method in the parent class 'product' echo "Manufacturer is " + manufacturer + ", price is " + price endproc endclass
Scope resolution operator
The scope resolution operator :: can be used to reference static methods and properties in classes.
class myclasslib public static mydata = array() public static proc display(arg) echo arg endproc endclass
We can call the display method without instantiating an object because it is declared static e.g.
myclasslib::display("hello world")
We can access "mydata" without instantiating an object because it is declared static e.g.
myclasslib::mydata("key") = "value"
Special variables
There are several special built-in object variables that can be used.
Object | Description |
---|---|
this | A reference to the currently executing object |
Iterating through object properties
You can iterate over the properties of an object using the foreach command like this.
// create a new object myobject = new myclass() // iterate over its properties foreach myobject as name => value echo "name=" + name + ", value=" + value // displays "name=MYPROP, value=hello world" endfor
Dynamically adding and removing properties runtime
As Recital is a dynamic language, object properties can be added and removed dynamically at runtime.
// create a new object myobject = new myclass() // extend it by adding a property at runtime myobject.addproperty("date", date()) // now remove it myobject.removeproperty('date')
Dynamically assigning methods at runtime
As Recital is a dynamic language, methods can be assigned to objects dynamically at runtime.
// create a new object myobject = new myclass() // dynamically add a method procedure mynewmethod() echo "hello world" endproc myobject.newmethod = mynewmethod myobject.newmethod() // displays "hello world"
Runtime data-type checking
You can restrict what types of data can be assigned to properties using the "as datatype" clause.
Type | Description |
---|---|
character | can assign character data |
numeric | can assign numeric data |
date | can assign date data |
logical | can assign logical data |
datetime | can assign datetime data |
currency | can assign currency data |
class | Name of user-defined class |
define class myclass public myprop as character = “Hello world” public procedure mymethod() return myprop endproc static procedure mystaticmethod() endproc enddefine // create a new object myobject = new myclass() myobject.myprop = "this is data" // works ok myobject.myprop = 10 // throws an error because we defined "myprop as character"
Understanding member lookup
Recital variables and procedures are declared in a hashed, block structured symbol table. Each procedure, function or method that is executed increases the runtime execution "level". Any variables or procedures that are referenced are looked up in decreasing runtime execution level order until found. In the case of object methods, the Recital runtime engine will first look in the active object for properties and methods. If not found, then it will carry out a standard symbol table lookup in decreasing runtime level order. This technique allows classes to be easily integrated into an existing application, and provides the ability for object methods to reference global variables and call global procedures.
The life of an object
Recital uses object reference counting to handle automatic garbage collection.
When an object is created its reference count is set to 1. All assignments of objects to other variables or procedure/function arguments cause the reference count to be incremented. An object is released from memory (dereferenced) when its reference count reaches zero.
To dereference an object you simply assign another value to it. e.g.
// create a new object (and set its reference count to 1) myobject = new myclass() // now free up the object. This will decrement the reference count associated with the object. // When the reference count is 0 then the object will be released from memory (dereferenced). myobject = null // In this example the object is not released as it is still referenced by 'myvar' myobject = new myclass() myvar = myobject myobject = null // now it will be released myvar = null
If an object property (variable) is an object reference, when the object containing the property (variable) is released (dereferenced) than the reference count of all object variables is decremented, and when the reference count reaches zero, then that object is released from memory.
Writing and using class libraries
Once you have completed the development of your class library you can use the classes within your programs like this.
require_once("myclasslib.prg")
The require_once( ) function will include the specified class library into your program only if it has not already been included and execute it at the current execution level. This will make all classes (also variables, procedures and functions) available to the calling program and all programs or procedures that it calls.
The specified class library is compiled into a Recital object file and loaded into shared global memory. This provides for optimal memory usage and also improved application performance as the class library is only loaded once. When class/procedure libraries (and in fact all Recital web .rsp files) are loaded like this in Recital Web them memory usage will be reduced dramatically.