Boa tarde...
existe alguma maneira de criar um novo method em uma classe existente, sem usar a herança?
um exemplo, gostaria de adicionar um method a class tget
method copiar(...) class tget
....
Return Self
o pq disso? para não ter que pegar o .prg da nova versão e alterando e compilando e assim por diante.
agradeço desde já.
Acrescentar um novo method a classe
Moderador: Moderadores
-
alxsts
- Colaborador

- Mensagens: 3092
- Registrado em: 12 Ago 2008 15:50
- Localização: São Paulo-SP-Brasil
Acrescentar um novo method a classe
Olá!
O material abaixo foi extraído do manual do xHarbour (xHarbour Language Reference Guide) e acredito que valha também para Harbour:
O material abaixo foi extraído do manual do xHarbour (xHarbour Language Reference Guide) e acredito que valha também para Harbour:
EXTEND CLASS...WITH METHOD
Adds a new method to an existing class.
Syntax
EXTEND CLASS <ClassName> WITH ;
[ MESSAGE <MessageName>] METHOD <FunctionName>
or
EXTEND CLASS <ClassName> WITH ;
MESSAGE <MessageName> INLINE <expression>
Arguments
<ClassName>
This is the symbolic name of an existing class to add a new method to.
MESSAGE <MessageName>
This is the symbolic name of the message which invokes the new method.
METHOD <FunctionName>
This is the symbolic name of the function implementing the code for the new method. If <MessageName> is omitted, <FunctionName> defines also the message that must be sent to an object for invoking the method.
INLINE <expression>
This is the expression which is executed when an INLINE method is invoked. <expression> must be one line of code. The code cannot contain commands but only function and method calls.
Description
The EXTEND CLASS...WITH METHOD statement allows for adding new methods to a declared class outside the class declaration. This way, a class can be enhanced even if the source code of the class is not available. The EXTEND CLASS statemend can modify all classes existing in xHarbour.
The new method is either implemented as a STATIC FUNCTION or as an INLINE expression. The self object is retrieved in both cases with function HB_QSelf().
Note: an alternative for enhancing an existing class is to use the FROM option of the CLASS statement and create a sub-class of an existing class.
ExampleCódigo: Selecionar todos
// The example adds a method to the "root" class of xHarbour: HBObject(). // All new classes inherit from this class. This is used in the example // for tracing variables depending on their data types in different log // files. #include "HbClass.ch" PROCEDURE Main LOCAL obj, nValue := 2, dDate := Date() EXTEND CLASS HBObject WITH MESSAGE log METHOD LogData ENABLE TYPE CLASS ALL obj := Test():new( "xHarbour" ) obj:log( "Objects.log" ) nValue:log( "Numerics.log" ) dDate:log( "Dates.log" ) RETURN CLASS Test EXPORTED: DATA value METHOD init(x) INLINE (::value := x, self) ENDCLASS STATIC FUNCTION LogData( cLogFile ) LOCAL self := HB_QSelf() LOCAL cOldFile IF .NOT. Set( _SET_TRACE ) RETURN self ENDIF IF Valtype( cLogFile ) == "C" cOldFile := Set( _SET_TRACEFILE, cLogFile ) ENDIF Tracelog( self ) IF cOldFile <> NIL Set( _SET_TRACEFILE, cOldFile ) ENDIF RETURN self
[]´s
Alexandre Santos (AlxSts)
Alexandre Santos (AlxSts)
-
aferra
- Usuário Nível 1

- Mensagens: 41
- Registrado em: 30 Mai 2008 06:55
- Localização: Ribeirão Preto/SP
Acrescentar um novo method a classe
agradecido, estudando e depois retorno com o resultado...
vlw Alexandre.
vlw Alexandre.