unit Unit8;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm8 = class(TForm)
mmo1: TMemo;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//类类型的使用
TA = class
protected
Fa: integer;
public
constructor Create; virtual; //要是不加virtual的话,可以看看区别
property a: Integer read Fa default 1;
end;
TAClass = class of TA;
TB = class(TA)
public
constructor Create; override;
end;
TC = class(TA)
public
constructor Create; override;
end;
var
Form8: TForm8;
const
TBCarray: array[0..1] of TAClass = (TB, TC);
implementation
{$R *.dfm}
procedure TForm8.btn1Click(Sender: TObject);
var
i: integer;
tmpObject: TA;
begin
for i := 0 to 1 do
begin
tmpObject:= TBCarray[i].Create;
mmo1.Lines.Add('tmpObject.ClassName: ' + tmpObject.ClassName);
mmo1.Lines.Add('tmpObject.a: '+IntToStr(tmpObject.a));
end;
end;
{ TA }
constructor TA.Create;
begin
Form8.mmo1.Lines.Add('TA.Create');
Fa:= 1;
end;
{ TB }
constructor TB.Create;
begin
inherited Create;
Form8.mmo1.Lines.Add('TB.Create');
Fa:= 2;
end;
{ TC }
constructor TC.Create;
begin
Form8.mmo1.Lines.Add('Tc.Create');
Fa:= 3;
end;
end.