有一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,请教各位!
解决方案 »
- BDE 怎样连接 数据库非默认1433 端口
- 关于随机抽样算法,高手进来直点下
- 广播的简单问题,来者有分.
- listview加载数据的速度特别慢
- 数据库备份与恢复的问题
- sql數據庫問題
- 大家帮忙呀(很急)!!!!
- 在线等待!用SaveDialog存文件时,如何让对话框不显示出来?参与者都有分!!!!
- 求批量导入EXCEL数据到SQL 2000的好方案。如10列数量100W条记录,只有这么多分了
- 请问在数据库中插入一条记录时,如果关键字重复,产生一个异常提示'Key Violation',这是一个什么异常?怎么样把这个异常的提示改成中文提示?
- 关于idHttp的几个问题,请各位帮忙解答一下!先谢了!
- 我的烦恼,chart疑问,帮我看看
unit Unit2;interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type TItems = class(TComponent)
public
end; TMyControl = class(TComponent)
private
FItems: TItems;
public
constructor Create(AOwner: TComponent); override;
property Items: TItems read FItems ;
end;
TItemsEx = class(TItems)
private
FStr: string;
public
property Str: string read FStr write FStr;
end; TMyControlEx = class(TMyControl)
public
constructor Create(AOwner: TComponent);override;
published
property Items: TItems read FItems write FItems;
end;
implementation{ TMyControl }constructor TMyControl.Create(AOwner: TComponent);
begin
inherited;
FItems := TItems.Create(Self);
end;
constructor TMyControlEx.Create(AOwner: TComponent);
var
litem : TItems;
begin
inherited;
litem := Items;
litem.Free; litem := TItemsEx.Create(Self);
end;end.
var
ab: TMyControl;
begin
AB := TMyControlEx.Create(nil);
ShowMessage(ab.Items.ClassName)
end;调用的例子
yq3woaini(哈哈镜 http://shop33546080.taobao.com(选择好心情!) ( TItems的定义和我自己的TItemsEx不是在同一unit中的,所以不能使用你的方法,并且TMyControl里是published
property Items: TItems read FItems write SetItems;
并且SetItems是这样定义的:
procedure TMyControl.SetItems(const Value: TItems);
begin
FItems.Assign(Value);
end;而不是property Items: TItems read FItems write FItems;
constructor TMyControlEx.Create(AOwner: TComponent);
begin
inherited;
Items.Free;
PPointer(@Items)^ := TItemsEx.Create(Self);
end;
如果用基类调用的话,还是调用基类的那个属性,用起来有局限性,不知道能不能满足你的要求
==============
另外一种方法是,在父类的构造函数中指定实现Item属性的类,
Constructor Create(AOwner: tComponent; ImpClass: TClass); Reintroduce;
begin
FItems:= ImpClass.Create;
...........
end;在继承类中以新的TItemEx来放在ImpClass的位置.
这种方法在Delphi的接口技术中很常用的,效果也不错
===========================================
取舍自己看着办吧,