xHarbour Reference Documentation > Statement Reference |
Declares the symbolic name of a method.
METHOD <MethodName>[(<params,...>] )] [INLINE <expression>] [SYNC]
When the METHOD statement occurs in the class declaration between CLASS and ENDCLASS, it declares the symbolic name of a method. This name must be sent to an object to invoke the method.
The program code for a declared method must be implemented with a separate METHOD declaration that follows the ENDCLASS statement. If, however, the method code can be programmed in one line, method declaration and implementation can be coded within the class declaration using the INLINE option. INLINE methods are not implemented separately from the class declaration.
When a method is invoked, the object executing the method is accessible in the code with the reserved variable name self. This can be abbreviated with two colons (::).
See also: | ACCESS, ASSIGN, CLASS, CLASSMETHOD, DATA, DELEGATE, EXPORTED:, HIDDEN:, INLINE METHOD, MESSAGE, METHOD (implementation), METHOD...OPERATOR, METHOD...VIRTUAL, PROTECTED:, StartThread(), VAR |
Category: | Class declaration , Declaration , xHarbour extensions |
Header: | hbclass.ch |
Source: | vm\classes.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
METHOD declaration
// The example declares a simple class with a regular and an // INLINE method. Note that the INLINE method consists of two // expressions. The first is an assignment and the second is the // return value of the method. Both expressions are comma separated // and enclosed in parentheses. #include "Hbclass.ch" PROCEDURE Main LOCAL obj CLS obj := Test():new( "xharbour" ) obj:print() RETURN CLASS Test HIDDEN: VAR name EXPORTED: METHOD init( cName ) INLINE ( ::name := cName, self ) METHOD print ENDCLASS METHOD print CLASS Test ? ::name RETURN self
SYNC method
// The example implements a Queue class that adds a value to a queue // with method :put(). Method :get() retrieves the last value added to // the queue. The queue itself is an array. Both methods are declared with // the SYNC attribute to protect the queue array from simultaneous access // by multiple threads. // // The Main thread puts values into the queue, while four other threads // retrieve values from the queue. #include "hbclass.ch" PROCEDURE Main LOCAL i, aT[4], oQueue := Queue():new() CLS FOR i:=1 TO 4 aT[i] := StartThread( @Test(), oQueue ) NEXT FOR i:=1 TO 4000 // Write values oQueue:put(i) // to queue NEXT WaitForThreads() // Wait until all threads RETURN // have terminated ** This code runs simultaneously in 4 threads PROCEDURE Test( oQueue ) LOCAL i, nRow, nCol nRow := GetThreadID() + 10 nCol := 0 // Read values FOR i:=1 TO 1000 // from queue DispOutAt( nRow, nCol+20, oQueue:nCount ) DispOutAt( nRow, nCol+40, oQueue:get() ) NEXT RETURN ** class with SYNC methods protecting the :aQueue array CLASS Queue PROTECTED: VAR aQueue INIT {} EXPORTED: VAR nCount INIT 0 READONLY METHOD put() SYNC METHOD get() SYNC ENDCLASS METHOD put( xValue ) CLASS Queue AAdd( ::aQueue, xValue ) ::nCount ++ RETURN self METHOD get() CLASS Queue LOCAL xReturn IF ::nCount > 0 ::nCount -- xReturn := ::aQueue[1] ADel ( ::aQueue, 1 ) ASize( ::aQueue, ::nCount ) ENDIF RETURN xReturn
http://www.xHarbour.com