自定义控件MyComponent有一个属性Lines,属于TString型。
我在代码中实现如下:
步骤1:申明部分。
TMyComponent= class(TTCustomEdit)
private
FLines: TStrings;
...
published
property Lines: TStrings read FLines write SetLines;
... end;
步骤2:实现部分
1:在构造函数中Create它
if not Assigned(FLines) then
Flines := TStrings.Create;2:procedure TMyComponent.SetLines(const Value: TStrings);
begin
FLines.Assign(Value);
end;步骤3:编译通过,打包安装。现象:Lines属性出现在Object Inspector中,当想点击它并且设置它的Lines属性时(类似于Memo的Lines),弹出Error框,abstract Error!我的分析:我跟踪过Memo的Lines(Memo的Lines属性是一直跟踪到TCustomEdit中才有申明,后面派生的类是一路的继承。),发现它的Lines属性也是Tstrings型的,但是它不是直接从TStrings继承过来,而是从TStrings上产生了一个新的类,TMemoStrings,申明如下:
TMemoStrings = class(TStrings)
private
Memo: TCustomMemo;
protected
...
public
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Insert(Index: Integer; const S: string); override;
end;请注意它这里有一个Private是Memo,它居然是TCustomMemo型,OH MY GOD这不是又绕回去了么?TCunstomMemo继承TCustomEdit,FLines是TCustomEdit的一个私有变量,FLines是TCustomMemo型。 这是怎么回事!!! 这是疑问1:疑问2:定义控件的String、或者Int属性时非常方便,难道每次定义TStrings属性都得这么绕么?好像这有点不符合OOP的思想,那么它又该如何定义呢? ----欢迎讨论,如果有精辟的见解,阿愿意另外送分!
我在代码中实现如下:
步骤1:申明部分。
TMyComponent= class(TTCustomEdit)
private
FLines: TStrings;
...
published
property Lines: TStrings read FLines write SetLines;
... end;
步骤2:实现部分
1:在构造函数中Create它
if not Assigned(FLines) then
Flines := TStrings.Create;2:procedure TMyComponent.SetLines(const Value: TStrings);
begin
FLines.Assign(Value);
end;步骤3:编译通过,打包安装。现象:Lines属性出现在Object Inspector中,当想点击它并且设置它的Lines属性时(类似于Memo的Lines),弹出Error框,abstract Error!我的分析:我跟踪过Memo的Lines(Memo的Lines属性是一直跟踪到TCustomEdit中才有申明,后面派生的类是一路的继承。),发现它的Lines属性也是Tstrings型的,但是它不是直接从TStrings继承过来,而是从TStrings上产生了一个新的类,TMemoStrings,申明如下:
TMemoStrings = class(TStrings)
private
Memo: TCustomMemo;
protected
...
public
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Insert(Index: Integer; const S: string); override;
end;请注意它这里有一个Private是Memo,它居然是TCustomMemo型,OH MY GOD这不是又绕回去了么?TCunstomMemo继承TCustomEdit,FLines是TCustomEdit的一个私有变量,FLines是TCustomMemo型。 这是怎么回事!!! 这是疑问1:疑问2:定义控件的String、或者Int属性时非常方便,难道每次定义TStrings属性都得这么绕么?好像这有点不符合OOP的思想,那么它又该如何定义呢? ----欢迎讨论,如果有精辟的见解,阿愿意另外送分!
FLines := TMemoStrings.Create;
TMemoStrings(FLines).Memo := Self;
并没有创建TCustomMemo,只是一个连接
就好像Tmemo有一个Parent属性指向他所在的窗体一样
TSTrings是抽象类
constructor TCustomMemo.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 185;
Height := 89;
AutoSize := False;
FWordWrap := True;
FWantReturns := True;
FLines := TMemoStrings.Create;
TMemoStrings(FLines).Memo := Self;
end;destructor TCustomMemo.Destroy;
begin
FLines.Free;
inherited Destroy;
end;
我觉得你是在哪里忘记写某句话了