W
wyismail
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TObj = class //TObject是一个类,而且是最基本的类。
end;
TClass = class of TObj;// TClass是类定义。
TA = class
end;
TAclass = class of TA;
TB = class
end;
TBclass = class of TB;
TForm1 = class(TForm)
private
{ Private declarations }
public
function MyCreateObject(C:TClass): TObject;
procedure Test;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.MyCreateObject(C: TClass): TObject;
begin
Result := C.Create;
end;
procedure TForm1.Test;
var O: TObject;
begin
O:= MyCreateObject(TAclass);
ShowMessage(O.ClassName);
O.Free;
O:= MyCreateObject(TBclass);
ShowMessage(O.ClassName);
O.Free;
end;
end.
错误:
[Error] Unit1.pas(52): '(' expected but ')' found
[Error] Unit1.pas(53): Incompatible types: 'TClass' and 'TAclass'
[Error] Unit1.pas(55): '(' expected but ')' found
[Error] Unit1.pas(56): Incompatible types: 'TClass' and 'TBclass'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TObj = class //TObject是一个类,而且是最基本的类。
end;
TClass = class of TObj;// TClass是类定义。
TA = class
end;
TAclass = class of TA;
TB = class
end;
TBclass = class of TB;
TForm1 = class(TForm)
private
{ Private declarations }
public
function MyCreateObject(C:TClass): TObject;
procedure Test;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.MyCreateObject(C: TClass): TObject;
begin
Result := C.Create;
end;
procedure TForm1.Test;
var O: TObject;
begin
O:= MyCreateObject(TAclass);
ShowMessage(O.ClassName);
O.Free;
O:= MyCreateObject(TBclass);
ShowMessage(O.ClassName);
O.Free;
end;
end.
错误:
[Error] Unit1.pas(52): '(' expected but ')' found
[Error] Unit1.pas(53): Incompatible types: 'TClass' and 'TAclass'
[Error] Unit1.pas(55): '(' expected but ')' found
[Error] Unit1.pas(56): Incompatible types: 'TClass' and 'TBclass'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'