//要点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.

相关文章:

  • 2022-12-23
  • 2021-05-31
  • 2022-02-20
  • 2021-10-12
猜你喜欢
  • 2021-09-25
  • 2022-02-05
  • 2021-07-23
相关资源
相似解决方案