Delphi函数详解:全局函数,内部函数,类的成员函数,类的静态方法

1. Delphi中的全局函数
//要点: 需要给其他单元调用, 必须在 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); //注意此时MyFun()是个全局函数
  ShowMessage(IntToStr(i)); {3}
end;
end.

2. Delphi中的内部函数---- unit中implementation区中的内部函数
//要点: 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;
var
  Form1: TForm1;
implementation
{$R *.dfm}
{implementation 区中的过程或函数, 只能在本单元调用}
function MyFun(x,y: Integer): Integer; //此时这个函数仅供本unit调用
begin
  Result := x + y;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  i := MyFun(1,2); //注意此时MyFun()是个全局函数
 end;
end.


 
3. Delphi中的内部函数---- Procedure/Function中的内部函数
//Delphi 支持过程中的方法
 procedure TForm1.Button1Click(Sender: TObject);   
    procedure alert(s: string);  
    begin     
    ShowMessage(s);   
    end; 
begin   
  alert('Hello'); 
end; 

4. Delphi类的成员函数
//要点: 如果声明在 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); //相当于C++中TForm1::Button1Click();
var   i: Integer; begin   i := MyFun(1,2);//类的实现内部当然可以调用类的方法MyFun()了   ShowMessage(IntToStr(i)); {3} end;
end.
  
5. Delphi的类方法---类似于C++中类的静态方法

//类方法就是通过类名就可以访问的方法
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;
{类方法示例:}
TMyClass = class(TObject)
  class procedure alert(s: string); {类方法只是比静态方法多了一个 class 指示字}
end;
{
  类方法不能在 private 和 protected 区;
  类方法不能是虚方法;
  类方法只能使用类中的、在对象实例化以前的数据.
}
var
  Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
class procedure TMyClass.alert(s: string);
begin
  ShowMessage(s);
end;
{类方法可以直接使用}
procedure TForm1.Button1Click(Sender: TObject);
begin
  TMyClass.alert('Hello world'); 
end;
{类的对象当然也能使用}
procedure TForm1.Button2Click(Sender: TObject);
var
  MyClass: TMyClass;
begin
  MyClass := TMyClass.Create;
  MyClass.alert('Hello World');  
  MyClass.Free;
end;
end.
--------------------------------------------------------------------------------
//静态类方法
{现在的 Delphi 不仅仅有类方法, 同时有: 
  类变量: class var
  类常量: class const
  类类型: class type
  类属性: class property
  静态类方法就是给类属性来调用的, 它可以存在与私有区(private), 
  譬如下面的 SetName 就是一个静态类方法:
}
TMyClass = class(TObject)
  private
    class var FName: string;
    class procedure SetName(const Value: string); static; {静态类方法又多了一个 static 指示字}
  published
  class property Name: string read FName write SetName;
end;

6. Delphi中指针函数参数

{现在这个函数并没有 var 前缀, 也就是说参数应该不会被修改的}  
function MyFun(p: PInteger): Integer; {PInteger 是 Integer 的指针类型}
begin
  p^ := p^ * 2;
  Result := p^;
end;

{测试}
procedure TForm1.Button1Click(Sender: TObject);
var
  i,x: Integer;
begin
  i := 8;
  x := MyFun(@i);           {调用函数}
  ShowMessage(IntToStr(x)); {16}
  {现在 i 的值应该不会被修改, 但...}
  ShowMessage(IntToStr(i)); {16}
{
  没有 var 或 out 前缀的参数, 应该是传值的;
  有 var 或 out 的参数是传地址的;
  
  指针就是一个地址, 尽管没有指定传地址, 但事实上就是给了一个地址, 
  所以参数值也会改变!
}
end;
原文地址:https://www.cnblogs.com/jijm123/p/9973313.html