假设
 str1 := 'TObject1';
 str2 := 'TObject2';
 ....
 ....
如何根据类名来创建类的实例,比如CreateObject就可以根据类名来创建类实例,不过它创建的是Com对象,我不需要Com对象,我需要是普通的Delphi类,望各位高手赐教!

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        ff:String;    constructor Create(AOwner: TComponent); override;
      end;  TForm1Class = Class of TForm1;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }constructor TForm1.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);  FF:='Test';
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      a:TForm1Class;
      b:TForm1;
    begin
      a:=TForm1Class(GetClass('TForm1'));
      if a=nil then exit;  b:=a.Create(nil);
      showmessage(b.ff);
    end;Initialization
      RegisterClass(TForm1);end.
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TTestClass = class of TTest;
      TTest = class(TPersistent)
      public
        function GetName: string;
      end;
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      RegisterClass(TTest);
      Caption :=  TTest(FindClass('TTest').Create).getName;
    end;{ TTest }function TTest.GetName: string;
    begin
      Result := 'hdhhd';
    end;end. firstshine(黑里透红)和我的方法一样,嘿嘿!结贴!