类定义单元
unit Unit2;interfaceuses Dialogs, Sysutils;
type
TMyclass = class(Tobject)
private
m_a:integer;
m_b:string;
public
procedure GetA(i:integer);
function GetB(s:String):String;
end;
implementation
{ TMyclass }procedure TMyclass.GetA(i: integer);
begin
m_a := i;
ShowMessage(IntToStr(i));
end;function TMyclass.GetB(s: String): String;
begin
ShowMessage(s);
m_b := s;
result := s;
end;end.调用类的单元:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to 3 do
begin
with TMyClass.Create do
begin
try
GetA(i);
GetB('sss');
finally
free;
end;
end;
end;
end;end.
unit Unit2;interfaceuses Dialogs, Sysutils;
type
TMyclass = class(Tobject)
private
m_a:integer;
m_b:string;
public
procedure GetA(i:integer);
function GetB(s:String):String;
end;
implementation
{ TMyclass }procedure TMyclass.GetA(i: integer);
begin
m_a := i;
ShowMessage(IntToStr(i));
end;function TMyclass.GetB(s: String): String;
begin
ShowMessage(s);
m_b := s;
result := s;
end;end.调用类的单元:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to 3 do
begin
with TMyClass.Create do
begin
try
GetA(i);
GetB('sss');
finally
free;
end;
end;
end;
end;end.
Frm : TForm;
begin
Frm := TForm.Create(Application); //创建
Frm.方法; //使用
Frm.Free; //释放
end;
var
B:TButton;
begin
B:=TButton.Create(Form1);
B.Parent:=Form1;
B.Caption:='新建的';
B.Left:=Width div 2;
B.Top :=Height div 2;
end;