Delphi 继承类的构造函数和析构函数的构建顺序

举例

Application: TAPPlication;

构建顺序: 1、构造函数是先继承父类的,初始化父类的成员通过Inherited。

2、析构函数是先释放本对象的,然后在释放父类的成员。s

constructor TApplication.Create(AOwner: TComponent);

var

P: PChar;

ModuleName: array[0..255] of Char;

begin

inherited Create(AOwner);

if not Assigned(Classes.ApplicationHandleException) then

Classes.ApplicationHandleException := HandleException;

if not Assigned(Classes.ApplicationShowException) then

Classes.ApplicationShowException := ShowException;

FBiDiMode := bdLeftToRight;

FTopMostList := TList.Create;

FWindowHooks := TList.Create;

FHintControl := nil;

FHintWindow := nil;

FHintColor := DefHintColor;

FHintPause := DefHintPause;

FHintShortCuts := True;

FHintShortPause := DefHintShortPause;

FHintHidePause := DefHintHidePause;

FShowHint := False;

FActive := True;

FAutoDragDocking := True;

FIcon := TIcon.Create;

{$IFDEF MSWINDOWS}

FIcon.Handle := LoadIcon(MainInstance, 'MAINICON');

{$ENDIF}

FIcon.OnChange := IconChanged;

GetModuleFileName(MainInstance, ModuleName, SizeOf(ModuleName));

OemToAnsi(ModuleName, ModuleName);

P := AnsiStrRScan(ModuleName, '\');

if P <> nil then StrCopy(ModuleName, P + 1);

P := AnsiStrScan(ModuleName, '.');

if P <> nil then P^ := #0;

AnsiLower(CharNext(ModuleName));

FTitle := ModuleName;

if not IsLibrary then CreateHandle;

UpdateFormatSettings := True;

UpdateMetricSettings := True;

FShowMainForm := True;

FAllowTesting := True;

FTestLib := 0;

ValidateHelpSystem;

HookSynchronizeWakeup;

end;

destructor TApplication.Destroy;

type

TExceptionEvent = procedure (E: Exception) of object;

var

P: TNotifyEvent;

E: TExceptionEvent;

begin

UnhookSynchronizeWakeup;

P := HandleException;

if @P = @Classes.ApplicationHandleException then

Classes.ApplicationHandleException := nil;

E := ShowException;

if @E = @Classes.ApplicationShowException then

Classes.ApplicationShowException := nil;

if FTestLib <> 0 then

FreeLibrary(FTestLib);

FActive := False;

CancelHint;

ShowHint := False;

inherited Destroy;

UnhookMainWindow(CheckIniChange);

if (FHandle <> 0) and FHandleCreated then

begin

if NewStyleControls then SendMessage(FHandle, WM_SETICON, 1, 0);

DestroyWindow(FHandle);

end;

if FHelpSystem <> nil then FHelpSystem := nil;

{$IFDEF LINUX}

if FObjectInstance <> nil then WinUtils.FreeObjectInstance(FObjectInstance);

{$ENDIF}

{$IFDEF MSWINDOWS}

if FObjectInstance <> nil then Classes.FreeObjectInstance(FObjectInstance);

{$ENDIF}

FWindowHooks.Free;

FTopMostList.Free;

FIcon.Free;

end;