目的是给TForm_SDALL类加一个新属性TblName:String,调用动态生成实例并给TblName赋值.//(1)单元文件内容
unit SDALL;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Buttons, Grids, DBGridEh;type
  TForm_SDALL = class(TForm)
  public
    constructor create;overload;
    TblName:String;
    constructor create(const str:String);overload;
  end;var
  Form_SDALL: TForm_SDALL;implementation
uses MAINMENU;{$R *.dfm}PROCEDURE TForm_SDALL.create(str:String);
begin
  inherited Create;
  TblName:=str;
end;end.
//(2)在另一个地方引用时出错
var
  myForm:TForm;
begin
   myForm:=TForm_SDALL.Create('SSS'); //这句编译时出错,说 Incompatible types: 'TComponent' and 'String'
end;

解决方案 »

  1.   

    TForm_SDALL = class(TForm)
      public
        constructor create(str:String);//这里改成这样
        TblName:String;
        constructor create(const str:String);overload;
      end;
      

  2.   

    constructor create(AOwner:TComponent;const str:String);reintroduce;PROCEDURE TForm_SDALL.create(AOwner:TComponent;str:String);
    begin
      inherited Create(AOwner);
      TblName:=str;
    end;
      

  3.   

    constructor create(AOwner:TComponent;const str:String);reintroduce;constructor TForm_SDALL.create(AOwner:TComponent;str:String);
    begin
      inherited Create(AOwner);
      TblName:=str;
    end;
      

  4.   

    谢谢二位回答!TO: bee2518(迷茫ing)
    按您的做了,问题依旧.TO: tonylk(=www.tonixsoft.com=) 
    按您的做了,调用也得跟着改成
    TForm_SDALL.Create(nil,'SSS'); 或者
    TForm_SDALL.Create(application,'SSS'); 
    都会出现 [Error]: Too many actual parameters .看起来,好象它还是在调用重载前的默认create,这个是只有一个参数的,而参数类型就为TComponent.
      

  5.   

    TO:  tonylk(=www.tonixsoft.com=) 
    按您的来,调用
    TForm_SDALL.Create(application,'SSS'); 
    也会出现 [Error]: Too many actual parameters
      

  6.   

    constructor create(AOwner: TComponent; const str:String); reintroduce;PROCEDURE TForm_SDALL.create(AOwner: TComponent; const str:String);
    begin
      inherited Create(AOwner);
      TblName:=str;
    end;
      

  7.   

    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm2 = class(TForm)
      private
        { Private declarations }
      public
        TblName:String;
        constructor create; reintroduce;overload;
        constructor create(const str:String);overload;
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}{ TForm2 }constructor TForm2.create;
    begin
      inherited Create(Application);
    end;constructor TForm2.create(const str: String);
    begin
      inherited Create(Application);
      TblName:=str;
    end;end.
      

  8.   

    注意,你的tblname 位置不对需要提到方法之前
      

  9.   

    最好你再定义一个FAowner属性将上面的
      inherited Create(Application);
    改为
      inherited Create(FAowner);
    这样就和原来完全兼容了
      

  10.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
      private
        { Private declarations }
      public
        { Public declarations }
        Constructor Create(Text:string); reintroduce ; overload;
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    Constructor TForm1.Create(Text:string);
    begin
      inherited Create(Application);
      showmessage(text);
    end;