D
dejoy
Unregistered / Unconfirmed
GUEST, unregistred user!
有一unit1定义如下:
type
TItems = class(TComponent)
public
end;
TMyControl = class(TComponent)
private
FItems: TItems;
public
constructor Create(AOwner: TComponent); override;
property Items: TItems read FItems ;
end;
implementation
{ TMyControl }
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited;
FItems := TItems.Create(Self);
end;
end.
另一个Unit2定义如下,从unit中的类继承:
type
TItemsEx = class(TItems)
private
FStr: string;
public
property Str: string read FStr write FStr;
end;
TMyControlEx = class(TMyControl)
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TMyControlEx }
constructor TMyControlEx.Create(AOwner: TComponent);
var
litem : TItems;
begin
inherited;
litem := Items;
litem.Free;
litem := TItemsEx.Create(Self);
end;
我想在不修改unit1的前提下(不能修改),继承它的类,把其中一个属性Items扩展替换为我自己的类TItemsEx,但用上面的方法不行,原来的Items成为nil了,litem成了一个临时变量,并没有替换为Items,请教各位!
type
TItems = class(TComponent)
public
end;
TMyControl = class(TComponent)
private
FItems: TItems;
public
constructor Create(AOwner: TComponent); override;
property Items: TItems read FItems ;
end;
implementation
{ TMyControl }
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited;
FItems := TItems.Create(Self);
end;
end.
另一个Unit2定义如下,从unit中的类继承:
type
TItemsEx = class(TItems)
private
FStr: string;
public
property Str: string read FStr write FStr;
end;
TMyControlEx = class(TMyControl)
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TMyControlEx }
constructor TMyControlEx.Create(AOwner: TComponent);
var
litem : TItems;
begin
inherited;
litem := Items;
litem.Free;
litem := TItemsEx.Create(Self);
end;
我想在不修改unit1的前提下(不能修改),继承它的类,把其中一个属性Items扩展替换为我自己的类TItemsEx,但用上面的方法不行,原来的Items成为nil了,litem成了一个临时变量,并没有替换为Items,请教各位!