//要点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.
转载于:https://my.oschina.net/hermer/blog/320168