Classes ... only with
Clipper
Clipper is a very
powerful language!
In my other page Clipper
Classes I show you how build your own class with Class.lib ; the same is
with oObject.lib except with the standard object inspector
InspectObject( <object>) )
named ::ClassInfo() in
oObject.lib.
But in oObject
is also included an header file ClipOO.ch (utilized
to build oObject.lib) that you can use to create your own classes without
any external libraries including oObject.
It's possible to write simple
or complex classes utilizing only this header file.
I show you how can do it with these
very simple prg.
Firstly
we need to create a module such us class.prg in which we must insert
the class definitions.
The
first line is to call class header:
#include "clipOO.ch"
then we have to create class:
#xTranslate o.nVal1 => Qself() \[\1]
#xTranslate o.nVal2 => Qself() \[\2]
CLASS Oper NAME Ope
CLASS DATA:
DATA nVal1, __nVal1
DATA nVal2, __nVal2
END DATA
CLASS METHODS:
METHOD New
METHOD Add , __Add
METHOD Sub , __Sub
METHOD Div , __Div
METHOD Mult, __Mult
END METHODS
ENDCLASS
The code
start with CLASS <name> and ends with ENDCLASS in order to
delimit start and end of class definition.
The
word DATA define the class variables.
METHOD
are definition for class methods.
When creating a new class, allthough class methods can be accessed
directly via the :: operator, the overhead created is great because the runtime system has to execute the appropriate function that
actually creates the class instance variable.
Instead it is better to create #xTranslate of the QSelf() elements e.g.
#xTranslate o.nVal1 => Qself() \[\1]
// is better than ::nVal1
#xTranslate o.nVal2 => Qself() \[\2] // is better than
::nVal2
First
method is the class constructor: in this case initialize the object with two
values into the class variables i.e the four calc operators.
The
other methods define the classic four operations in a calculator.
Then
we have to define the constructor method:
METHOD New( nVal1, nVal2 ) CLASS Ope
IF VALTYPE( nVal1 ) == "N"
o.nVal1 := nVal1
ENDIF
IF VALTYPE( nVal2 ) == "N"
o.nVal2 := nVal2
ENDIF
RETURN Self
That's
all you need to define a class.
We
beg you to note that we have utilized the o.nVal1 operator instead of
the symbol <::> i.e the send operator (see above)
We now define the other methods:
METHOD __Add () CLASS Ope
RETURN ( o.nVal1 + o.nVal2 )
METHOD __Sub () CLASS Ope
RETURN ( o.nVal1 - o.nVal2 )
METHOD __Div () CLASS Ope
RETURN ( o.nVal1 / o.nVal2 )
METHOD __Mult () CLASS Ope
RETURN ( o.nVal1 * o.nVal2 )
Please take note that you can utilize ::nVal1
insted of o.nVal1! But it's better o.nVal1 since it's much
faster.
And finally I initialize the VAR
datas:
DEFINE DATA __nVal1 OF o.nVal1 GETSET
DEFINE DATA __nVal2 OF o.nVal2 GETSET
very simple... or not?
In
a separate module we can utilize your private clipper class!
FUNCTION Test
//to create a new object in the new class
//we
assign two new operators in calculator: 4 and 5
LOCAL oObj := Oper():New( 5, 4 )
//the standard object inspector
InspectObject( oObj )
CLS
QOUT( "I verify type of variable <oObj>;" )
QOUT( VALTYPE( oObj ) )
QOUT()
QOUT()
QOUT( "I execute operations, utilizing these operators:" )
QOUT( oObj:nVal1, " and ", oObj:nVal2 )
QOUT( "" )
QOUT( "Sum :", oObj:Add )
QOUT( "Sub :", oObj:Sub )
QOUT( "Div :", oObj:Div )
QOUT( "Mult :", oObj:Mult )
QOUT( "" )
QOUT( "End of job" )
RETURN NIL
You
may download here the two files in order to compile it.
Remember
you dont need any library except the head file clipOO.ch by Manos
Aspradakis.
download example1.zip
Home page