自定义控件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.   

    TStrings是一个抽象类,实际上你需要Flines := TStringList.Create;来创建他而把它公布出来的时候作为TStrings正是OOP的精妙之处...也使多态的在编码中有了用武之地
      

  2.   

    至于TMemoStrings有一个private是Memo你可以看到TCustomMemo的Create里是这么写的
      FLines := TMemoStrings.Create;
      TMemoStrings(FLines).Memo := Self;
    并没有创建TCustomMemo,只是一个连接
    就好像Tmemo有一个Parent属性指向他所在的窗体一样
      

  3.   

    事实上我有Create它,而且这段代码我也看到了。
      

  4.   

    你出错的原因是 Flines := TStrings.Create;
    TSTrings是抽象类
      

  5.   

    你参看一下TCustomMemo的创建
    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;
    我觉得你是在哪里忘记写某句话了
      

  6.   

    TO:回复人: rustle() 问题解决了。大家如果不介意可以借题发挥一下,关于多态的问题。精辟的帖子我将另外给分。这50分已经属于rustle()了。