A
awfigsk
Unregistered / Unconfirmed
GUEST, unregistred user!
还是刘艺老师所写的编程思想这本书的第95页
unit Demo;
...
type
...
TWheel=class(TObject)
private
...
public
constructor create;
procedure start;
procedure stop;
end;
TVehicle=class(TObject)
protected
...
FWheels:TList;
end;
TBicycle=class(TVehicle)
public
constructor create;
destructor Destory;
...
end;
implementation
constructor TBicycle.create;
var
i:integer;
begin
...
FWheels:=TList.create;
for i:=1 to 2 do
FWheels.Add(TWheel.create(21,'B型自行车车轮',i));
end;
destructor TBicycle.destory;
var
i:Integer;
begin
for i:=1 to 2 do
TWheel(FWheels.Items).Free;
inherited;
end;
....
我想请问一下,为何在TBicycle.destory方法中,要加入
TWheel(FWheels.Items).Free
?
FWheels不是TBicycle父类TVehicle的数据成员吗,在TBicycle释放时,难道不会自动释放FWheels这个数据成员吗?
[red]是不是因为TVehicle类没有重写析构函数?[/red]
还请大侠们指点,谢谢!
unit Demo;
...
type
...
TWheel=class(TObject)
private
...
public
constructor create;
procedure start;
procedure stop;
end;
TVehicle=class(TObject)
protected
...
FWheels:TList;
end;
TBicycle=class(TVehicle)
public
constructor create;
destructor Destory;
...
end;
implementation
constructor TBicycle.create;
var
i:integer;
begin
...
FWheels:=TList.create;
for i:=1 to 2 do
FWheels.Add(TWheel.create(21,'B型自行车车轮',i));
end;
destructor TBicycle.destory;
var
i:Integer;
begin
for i:=1 to 2 do
TWheel(FWheels.Items).Free;
inherited;
end;
....
我想请问一下,为何在TBicycle.destory方法中,要加入
TWheel(FWheels.Items).Free
?
FWheels不是TBicycle父类TVehicle的数据成员吗,在TBicycle释放时,难道不会自动释放FWheels这个数据成员吗?
[red]是不是因为TVehicle类没有重写析构函数?[/red]
还请大侠们指点,谢谢!