xHarbour Reference Documentation > Statement Reference |
Replaces a method in an existing class.
OVERRIDE METHOD <MethodName> ; IN CLASS <ClassName> WITH <FunctionName>
The OVERRIDE METHOD statement replaces the implementation of a method in an existing class. When an object receives the message <MethodName>, the code implemented in <FunctionName> is executed and the previous implementation is no longer accessible. <FunctionName> is usually implemented as a STATIC FUNCTION where the self object is retrieved with HB_QSelf().
OVERRIDE METHOD can be used to replace the method implementation in a higher level of a class tree. This is especially useful when the source code of the classes is not available. However, great care must be taken in order not to break existing class inheritance.
An alternative is the declaration of a sub-class using the FROM option in the class declaration and implement the new method in the sub-class.
See also: | ASSOCIATE CLASS, DELEGATE, EXTEND CLASS...WITH METHOD, METHOD (implementation), OPERATOR |
Category: | Class declaration , Declaration , xHarbour extensions |
Header: | hbclass.ch |
Source: | vm\classes.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example outlines how a method can be replaced in existing // classes and how the self object is retrieved in the implementation // of a replacement method. #include "hbclass.ch" PROCEDURE Main() LOCAL cVar := "Hello", GetList := {} CLS OVERRIDE METHOD Display IN CLASS GET WITH MyGetDisplay OVERRIDE METHOD AsString IN CLASS ARRAY WITH MyArrayString @ 10,10 GET cVar ? GetList:AsString() RETURN STATIC FUNCTION MyGetDisplay LOCAl Self := HB_QSelf() Alert( ::VarGet() ) RETURN Self STATIC FUNCTION MyArrayString() LOCAl Self := HB_QSelf() RETURN "{ ... }"
http://www.xHarbour.com