请教各位大侠一个有关DELPHI中析构函数的问题???(100分)

  • 主题发起人 主题发起人 dolphin2001
  • 开始时间 开始时间
你错误理解Object Pascal
type
TWorkClass = class
private
FBaseClass:TBase_WorkClass;
public
procedure GotoWork;
public
constructor Create(BaseClass:TBase_WorkClass);
destructor Destroy
override
//跟踪其代码,没有去执行 FBaseClass 的相应派生类的析构函数???
end


FBaseClass只是TWorkClass的一个私有变量
constructor Create(BaseClass:TBase_WorkClass)
只是说TWorkClass建构时需要一个
类型为TBase_WorkClass的变量
destructor Destroy
override;说明TWorkClass重载父类的Destroy;
这三个叙述是一点关系都没有
 
type
TBase_WorkClass = class
private
FVersion :string;
procedure SetVersion(value:string);
function GetVersion:string;
public
procedure DoWork
virtual;abstract;
procedure DoPrepare
virtual ;abstract;
public
destructor Destroy
virtual ;
//这里重新宣告为virtual 会切断与TObject Destroy 的多形效果,执行上也会有错误 必须为
destructor Destroy;override;
published
property Version :string read GetVersion write SetVersion;
end;

type
TFirstClass = class(TBase_WorkClass)
private
FWorkList: TStringList;
FChainCount: Integer;
public
procedure DoWork
override;
procedure DoPrepare
override;
procedure SetChainLength(const ChainLength:Integer);
public
constructor Create;
destructor Destroy
override;
published
property Version;
end;

type
TSecondClass = class(TBase_WorkClass)
public
procedure DoWork
override;
procedure DoPrepare
override;
public
constructor Create;
destructor Destroy
override;
published
property Version;
end;


type
TWorkClass = class
private
FBaseClass:TBase_WorkClass;
public
procedure GotoWork;
public
constructor Create(BaseClass:TBase_WorkClass);
destructor Destroy
override
//跟踪其代码,没有去执行 FBaseClass 的相应派生类的析构函数???

end;

implementation
destructor TBase_WorkClass.Destroy;
begin

end;

function TBase_WorkClass.GetVersion: string;
begin
result := FVersion;
end;

procedure TBase_WorkClass.SetVersion(value:string);
begin
FVersion :=value;
end;

constructor TWorkClass.Create(BaseClass: TBase_WorkClass);
begin
FBaseClass :=BaseClass;
end;

destructor TWorkClass.Destroy;
begin
FBaseClass.free;
//这里会去执行 FBaseClass 的析构函数
// FBaseClass.Destroy;
FBaseClass :=nil;

inherited Destory
//在这里写不写都一样,只是习惯

end;

procedure TWorkClass.GotoWork;
begin
FBaseClass.DoPrepare;
FBaseClass.DoWork;
//TBase_WorkClass 的这两个函数宣告为抽象方法,必须在后代实作且在后代调用
// 这里会发生错误
end;

constructor TFirstClass.Create;
begin
inherited ;
FWorkList := TStringList.Create;
end;


destructor TFirstClass.Destroy;
begin
FWorkList.Free;
inherited Destroy;
end;

procedure TFirstClass.DoPrepare;
var
i: Integer;
begin
inherited;
// 这里调用的 TBase_WorkClass.DoPrepare 为抽象方法,会发生错误
for i := FChainCount downto 0 do
begin
FWorkList.Add(IntToStr(i));
end;
end;

procedure TFirstClass.DoWork;
var
TmpString:string;
ShowMsg: array [0..256] of char;
begin
inherited;
// 这里调用的 TBase_WorkClass.DoWork 为抽象方法,会发生错误
TmpString := 'Current Chain Length is '+IntToStr(FChainCount);
StrPCopy(ShowMsg,TmpString);
Application.MessageBox(ShowMsg,'信息',0);
end;

procedure TFirstClass.SetChainLength(const ChainLength: Integer);
begin
FChainCount := ChainLength;
end;

constructor TSecondClass.Create;
begin
inherited Destroy;
//这里输入错了吧? 应该
inherited Create;
end;

destructor TSecondClass.Destroy;
begin
inherited;

end;

procedure TSecondClass.DoPrepare;
begin
inherited;
// 和 TFirstClass.DoPrepare 的情况一样;
Application.MessageBox('Second Class ','SysInfo',0);
end;

procedure TSecondClass.DoWork;
begin
inherited;
// 和 TFirstClass.DoWork 的情况一样;
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部