Files
harbour-core/tests/objects.prg
vszakats a4a357a18b 2013-03-15 11:12 UTC+0100 Viktor Szakats (harbour syenar.net)
* /harbour/* -> /*
    * moved whole Harbour source tree one level up to
      avoid single 'harbour' top dir
2013-03-15 11:13:30 +01:00

66 lines
1.6 KiB
Plaintext

/*
* $Id$
*/
// Testing Harbour classes and objects management
// be aware Harbour provides a much simpler way using Class HBClass
#include "hboo.ch"
PROCEDURE Main()
LOCAL oObject := TAny():New()
? ValType( oObject )
? Len( oObject ) // 3 datas !
? oObject:ClassH() // retrieves the handle of its class
? oObject:ClassName() // retrieves its class name
oObject:Test() // This invokes the below defined Test function
// See QSelf() and :: use
? oObject:cName
oObject:DoNothing() // a virtual method does nothing,
// but it is very usefull for Classes building logic
RETURN
FUNCTION TAny() /* builds a class */
STATIC hClass
IF hClass == NIL
hClass := __clsNew( "TANY", 3 ) // cClassName, nDatas
__clsAddMsg( hClass, "cName", 1, HB_OO_MSG_DATA ) // retrieve data
__clsAddMsg( hClass, "_cName", 1, HB_OO_MSG_DATA ) // assign data. Note the '_'
__clsAddMsg( hClass, "New", @New(), HB_OO_MSG_METHOD )
__clsAddMsg( hClass, "Test", @Test(), HB_OO_MSG_METHOD )
__clsAddMsg( hClass, "DoNothing", 0, HB_OO_MSG_VIRTUAL )
ENDIF
/* warning: we are not defining datas names and methods yet */
RETURN __clsInst( hClass ) // creates an object of this class
STATIC FUNCTION New()
LOCAL Self := QSelf()
? ValType( Self )
? "Inside New()"
::cName := "Harbour OOP"
RETURN Self
STATIC FUNCTION Test()
LOCAL Self := QSelf() // We access Self for this method
? "Test method invoked!"
? ::ClassName() // :: means Self: It is a Harbour built-in operator
RETURN NIL