Code
//一般情况下, 类都会定义在 interface 区; 在 implementation 区定义的类只能本单元使用.
//------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
{类定义, 因为已经在 Type 区了, 可以省略 Type 关键字}
TMyClass = class
procedure MyProc(var x: Integer);
function MyFun (var x: Integer): Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{TMyClass 中方法的实现}
function TMyClass.MyFun(var x: Integer): Integer;
begin
x := x * 2;
Result := x;
end;
procedure TMyClass.MyProc(var x: Integer);
begin
x := x * 2;
end;
//调用测试
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
myClass: TMyClass; {对象声明}
begin
myClass := TMyClass.Create; {对象建立}
i := 6;
myClass.MyProc(i); {调用方法}
ShowMessage(IntToStr(i)); {12}
i := 6;
i := myClass.MyFun(i); {调用方法}
ShowMessage(IntToStr(i)); {12}
myClass.Free; {对象释放}
end;
end.
//一般情况下, 类都会定义在 interface 区; 在 implementation 区定义的类只能本单元使用.
//------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
{类定义, 因为已经在 Type 区了, 可以省略 Type 关键字}
TMyClass = class
procedure MyProc(var x: Integer);
function MyFun (var x: Integer): Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{TMyClass 中方法的实现}
function TMyClass.MyFun(var x: Integer): Integer;
begin
x := x * 2;
Result := x;
end;
procedure TMyClass.MyProc(var x: Integer);
begin
x := x * 2;
end;
//调用测试
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
myClass: TMyClass; {对象声明}
begin
myClass := TMyClass.Create; {对象建立}
i := 6;
myClass.MyProc(i); {调用方法}
ShowMessage(IntToStr(i)); {12}
i := 6;
i := myClass.MyFun(i); {调用方法}
ShowMessage(IntToStr(i)); {12}
myClass.Free; {对象释放}
end;
end.