理解 Delphi 的类(十) - 深入方法[15] - 调用其他单元的函数


//要点15: 调用其他单元的函数

//包含函数的单元:

unit Unit2;



interface



function MyFun(x,y: Integer): Integer; {函数必须在接口区声明}



implementation



function MyFun(x,y: Integer): Integer; {函数必须在函数区实现}

begin

  Result := x + y;

end;



end.





//调用函数的单元:

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;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses Unit2; {必须 uses 定义函数的单元}



procedure TForm1.Button1Click(Sender: TObject);

var

  i: Integer;

begin

  i := MyFun(1,2);         {调用函数}

  //i := Unit2.MyFun(1,2); {有时为了避免重名, 需要这样调用}

  ShowMessage(IntToStr(i)); {3}

end;



end.


 
   

你可能感兴趣的:(Delphi)