Inheritance is one of the features of object-oriented development. Good inheritance can effectively reduce code redundancy and improve code reuse. Delphi is an excellent object-oriented development tool, which is not weak in this aspect. When we create a new form in Delphi, we can see the following code:
Type
Tform1
= Class
(Tform)
Private
... {Private Declarations}
Public
... {Public declarations}
End;
This indicates that all the forms we created are inherited fromTformThis class. If we want all the forms in a project to have the same style and font standards, we do not have to specify each form one by one, you only need to define a standard form as the base class of All Forms in the project, in this way, you only need to define the form style and font standard in the form of this base class, and then apply it to all its subclass forms. Suppose we design an MDI project and want all the MDI forms to have the same interface layout, We can first create an MDI form base class and then let all the MDI forms be derived from this class, instead of the tform class.
Tfrmmdibase = Class (tform)
Coolbar1: tcoolbar;
Toolbar1: ttoolbar;
Toolbutton1: ttoolbutton;
Toolbutton2: ttoolbutton;
Toolbutton3: ttoolbutton;
Toolbutton4: ttoolbutton;
Toolbutton5: ttoolbutton;
Dsactive: tdatasource;
Toolbutton6: ttoolbutton;
Toolbutton7: ttoolbutton;
Toolbutton8: ttoolbutton;
Toolbutton9: ttoolbutton;
Toolbutton10: ttoolbutton;
Actionlist1: tactionlist;
Datasetfirst1: tdatasetfirst;
Datasetprior1: tdatasetprior;
Datasetnext1: tdatasetnext;
Datasetlast1: tdatasetlast;
Dbgrideh1: tdbgrideh;
Aqactive: tadoquery;
Actnew: taction;
Actedit: taction;
Actexit: taction;
Actdelete: taction;
Toolbutton11: ttoolbutton;
Actrefresh: taction;
Panel1: tpanel;
Pnledit: tpanel;
Actcancel: taction;
Toolbutton12: ttoolbutton;
Actsave: taction;
Toolbutton13: ttoolbutton;
Procedure formclose (Sender: tobject; var action: tcloseaction );
Procedure formcreate (Sender: tobject );
Procedure actnewexecute (Sender: tobject );
Procedure actdeleteexecute (Sender: tobject );
Procedure actexitexecute (Sender: tobject );
Procedure actnewupdate (Sender: tobject );
Procedure actdeleteupdate (Sender: tobject );
Procedure actexitupdate (Sender: tobject );
Procedure aqactiveafterinsert (Dataset: tdataset );
Procedure actrefreshupdate (Sender: tobject );
Procedure actrefreshexecute (Sender: tobject );
Procedure actcancelexecute (Sender: tobject );
Procedure actcancelupdate (Sender: tobject );
Procedure actsaveexecute (Sender: tobject );
Procedure actsaveupdate (Sender: tobject );
Procedure aqactiveposterror (Dataset: tdataset; E: edatabaseerror;
VaR action: tdataaction );
Private
... {Private Declarations}
Procedure opendataset;
Procedure closedataset;
Protected
Public
... {Public declarations}
End;
You can use keywords in subclassOverrideUsed to rewrite the ancestor classVirtualKeyword declaration virtual method. You can useInheritedKeyword.
Destructor tcollectionitem. Destroy;
Begin
Setcollection (NiL );
Inherited destroy;
End;
The usage of the dot model is the sandwich method mentioned in Li Wei's book "Inside VCL (deep core-VCL Architecture Analysis.
Destructor tstringlist. Destroy;
Begin
Fonchange: = nil;
Fonchanging: = nil;
Inherited destroy;
If fcount <> 0 then finalize (flist ^ [0], fcount );
Fcount: = 0;
Setcapacity (0 );
End;
In Delphi, there is no multi-Inheritance like C ++, that is, each class can only have one parent class. But in Delphi, you can also implement this multi-inheritance through interfaces. Specifically, this does not mean multi-inheritance, but it just looks like multi-inheritance. For example, tcomponent is the ancestor class of all components in Delphi.
Tcomponent
= Class
(Tpersistent, iinterface, iinterfacecomponentreference)
Private
............
... {Iinterfacecomponentreference}
Function iinterfacecomponentreference. getcomponent = intfgetcomponent;
Function intfgetcomponent: tcomponent;
Protected
............
... {Iinterface} function QueryInterface (const IID: tguid; out OBJ): hresult; virtual; stdcall;
Function _ addref: integer; stdcall;
Function _ release: integer; stdcall;
... {Idispatch}
Function gettypeinfocount (Out count: integer): hresult; stdcall;
Function gettypeinfo (index, localeid: integer; out typeinfo): hresult; stdcall;
Function getidsofnames (const IID: tguid; names: pointer;
Namecount, localeid: integer; DISPIDs: pointer): hresult; stdcall;
Function invoke (dispid: integer; const IID: tguid; localeid: integer;
Flags: word; var Params; varresult, interval info, argerr: pointer): hresult; stdcall;
Public
Constructor create (aowner: tcomponent); Virtual;
Destructor destroy; override;
............
Property comobject: iunknown read getcomobject;
Property components [index: integer]: tcomponent read getcomponent;
Property componentcount: integer read getcomponentcount;
Property componentindex: integer read getcomponentindex write setcomponentindex;
Property componentstate: tcomponentstate read fcomponentstate;
Property componentstyle: tcomponentstyle read fcomponentstyle;
Property designinfo: longint read fdesigninfo write fdesigninfo;
Property Owner: tcomponent read fowner;
Property vclcomobject: pointer read fvclcomobject write fvclcomobject;
Published
Property name: tcomponentname read fname write setname stored false;
Property Tag: longint read ftag write ftag default 0;
End;
Note: The last three sections of code are extracted from the class. Pas unit file of Delphi.