从TObject中继承的对象无法调用GetClass()函数,如何动态创建。
如定义一个新类:TLineObj = class(TObject);
....
如何使用 TObjectClass(GetClass('TLineObj')).Create(self)完成对象的生成。
因为只有从TPersistent继承的对象才支持RegisterClass()。
如定义一个新类:TLineObj = class(TObject);
....
如何使用 TObjectClass(GetClass('TLineObj')).Create(self)完成对象的生成。
因为只有从TPersistent继承的对象才支持RegisterClass()。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TLineObj=class(TPersistent)
end; TLineObjClass=class of TLineObj; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
LineObj:TLineObj;
begin
try
LineObj:=TLineObjClass(GetClass('TLineObj')).Create ;
finally
LineObj.Free;
end;
end;Initialization
RegisterClass(TLineObj);end.
现在只想探讨一下TObject继承的对象动态生成问题。例如:在MFC中从CObject继承的对象就可以动态生成。
如果MFC中Cobject可以的话,那可能因为他是用多重继承的吧