作为函数的入参,若是record类型,默认是值拷贝,效率低,若要传指针,需要加 var ;
作为函数的入参,若是 class类型,默认是传地址,不需要加var
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TDataWarp = class a: Integer; b: Integer; end; TForm1 = class(TForm) Button1: TButton; mmo1: TMemo; procedure swap2(Sender: TObject; dw: TDataWarp); procedure Button1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin ReportMemoryLeaksOnShutdown := True; end; procedure TForm1.swap2(Sender: TObject; dw: TDataWarp); var tmp: Integer; begin tmp := dw.a; dw.a := dw.b; dw.b := tmp; mmo1.Lines.Add(dw.a.ToString); dw := nil; dw.Free; end; procedure TForm1.Button1Click(Sender: TObject); var dw: TDataWarp; begin dw := TDataWarp.Create; dw.a := 6; dw.b := 9; swap2(Sender, dw); mmo1.Lines.Add(dw.a.ToString); dw.Free; end; end.