//要點13: 需要給其他單元調用, 必須在 interface 聲明, 但必須在 uses 區後面
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;
{需要給其他單元調用, 必須在 interface 聲明, 但必須在 uses 區後面}
function MyFun(x,y: Integer): Integer; {函數聲明}
var
Form1: TForm1;
implementation
{$R *.dfm}
function MyFun(x,y: Integer): Integer; {函數實現}
begin
Result := x + y;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
i := MyFun(1,2);
ShowMessage(IntToStr(i)); {3}
end;
end.
//要點14: 如果聲明在 TForm1 類內, 那它就是 TForm1 類的一個方法了
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
function MyFun(x,y: Integer): Integer; {函數聲明在 TForm1 類的體內}
{現在這個函數 MyFun 已經是 TForm1 類的一個方法了}
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{函數在實現區必須有 TForm1. 作為前綴}
function TForm1.MyFun(x,y: Integer): Integer;
begin
Result := x + y;
end;
{調用}
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
i := MyFun(1,2);
ShowMessage(IntToStr(i)); {3}
end;
end.