一般都是Create(AOwner:TComponent)一个AOWner参数,有没有可能
增加一个或两个参数,因为我在建立类时有可能根据不同情况,新建
出来的类的外观等根据参数会有所不同。这种情况除了在Create里加参数,还有没有别的办法??

解决方案 »

  1.   

    你要自己通过继承产生一个新的控件才可以,比如:
    TMyButton= class(TButton);
    private
      fColor: TColor;
    public
      constructor Create(AOwner: TComponent; Color: TColor); override;
    end;constructor TMyButton.Create(AOwner: TComponent; Color: TColor);
    begin
      Inherited Create(AOwner);
      fColor:=Color;
    end;这样新控件就有了新的参数了。
      

  2.   

    应该用reintroduce来覆盖原来的构造函数。但是这样一来,你的控件就无法在设计时进行编辑……因为IDE是无法自动调用你的新构造函数的。实际上并不推荐这么用,完全可以用其他的方法来初始化数据。比如自己定义一个事件,在构造函数中回调就行了。
      

  3.   

    在类里面重定(overload)一个create事件也可以的啊,你想把参数怎么变就怎么变
      

  4.   

    不可以的,要想自定义参数类型及个数,只有从TObject继承才行
      

  5.   

    使用constructor 作一个析构
      

  6.   

    使用自定参数的Constructor只能手动创建!对此Delphi IDE无能为力。
    citytramper(阿琪)的方法也是行的通的。对于可视控件,请尽量避免修改构造函数的参数结构。
      

  7.   

    要inHerit父类的建构函数,
    要保留Owner参数传递。除此之外没有多少好说的。同意 jufeng_zhu(老猪)
      

  8.   

    overload就可以了啊
    然后再
    inherited Create(将原来create的参数传到这里);
      

  9.   

    我那天就是这么做的,可是还是不行呀,应该是jufeng_zhu(老猪)的方法。unit ddgRunButton;interfaceuses
      SysUtils, Classes, Controls,StdCtrls,Buttons;type
      TddgRunButton = class(TWinControl)
      private
        FEdit:TEdit;
        FSpeedButton:TSpeedButton;
      protected
        procedure SetText(Value:string);
        function  GetText:string;
      public
     ///////////////declaration of 'Create' differ  from previous declaration////////
        constructor Create(AOwner:TComponent;BillType:integer);override;       published
        property Text:string read GetText write SetText;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('DDG', [TddgRunButton]);
    end;constructor TddgRunButton.Create(AOwner:TComponent;BillType:integer);
    begin
      inherited Create(AOwner);
      FEdit:=TEdit.Create(Self);
      FEdit.Parent:=Self;
      FEdit.Height:=21;  FSpeedButton:=TSpeedButton.Create(Self);
      FSpeedButton.Parent:=Self;
      FSpeedButton.Left:=FEdit.Width;
      FSpeedButton.Height:=19;
      FSpeedButton.Width:=19;
      FSpeedButton.Caption:='...';
      FSpeedButton.Parent:=Self;
    end;procedure TddgRunButton.SetText(Value:string);
    begin
      FEdit.Text:=Value;
    end;
    function TddgRunButton.GetText:string;
    begin
      Result:=FEdit.Text;
    end;end.这句出错
    constructor Create(AOwner:TComponent;BillType:integer);override; 
     ///////////////declaration of 'Create' differ  from previous declaration////////
      

  10.   

    在实现部分就不要加override了。。
      

  11.   

    可以在写完方法声明后,SHIFT+CTRL+C,类完成功能,自动生成实现部分。。
      

  12.   

    应该用reintroduce和overload
    程序:
    unit BillHead;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
      Dialogs, cxMaskEdit, cxButtonEdit, cxDBEdit, StdCtrls, cxControls,
      cxContainer, cxEdit, cxTextEdit;type
      TBillHead = class(TFrame)
        cxDBTextEdit1: TcxDBTextEdit;
      private
        { Private declarations }
      public
         constructor Create(AOwner:TComponent;BillType:integer);reintroduce;overload;
      end;implementation{$R *.dfm}{ TBillHead }constructor TBillHead.Create(AOwner:TComponent;BillType:integer);
    begin
      inherited Create(AOwner);
        showmessage(inttostr(Billtype));end;end.