修饰符(类篇)

1.无修饰符----传指针,指针被复制一份入栈。函数内修改属性值后,仅仅是修改堆中数据的值,并没有复制堆中的数据,这点与string不同,留意。

2.const 修饰符---传指针,指针被复制一份入栈。与无修饰符一致,据说加上const编译器会优化。可加可不加!!

 3.var修饰符-----直接把变量现在的内存编号传过去,就是说没有任何新指针或其它【入栈】。

 

 

 4.out修饰符----------与var一样,传递过来的是现在变量本身。

 

unit Unit4;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm4 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  /// <summary>
  /// 定义一个类
  /// </summary>
  TPerson = class
  private
    Fname: string;
    Fage: Integer;
    Fsex: Boolean;
    procedure Setage(const Value: Integer);
    procedure Setname(const Value: string);
    procedure Setsex(const Value: Boolean);
  public
    property name: string read Fname write Setname;
    property age: Integer read Fage write Setage;
    property sex: Boolean read Fsex write Setsex;
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure abc1(ap: TPerson);
begin
  ap.name := '李大嘴1';
end;

procedure abc2(const ap: TPerson);
begin
  ap.name := '李大嘴2';
end;

procedure abc3(var ap: TPerson);
begin
  ap.name := '李大嘴3';
end;

procedure abc4(out ap: TPerson);
var
  a: string;
begin
  a := ap.name;
  ap.name := '李大嘴4';
end;

procedure TForm4.Button1Click(Sender: TObject);
var
  ps: TPerson;
begin
  ps := TPerson.Create;
  ps.name := '小李飞刀';
  ps.age := 29;
  ps.sex := True;

  abc4(ps);
  Memo1.Lines.Add(ps.name);

  //一定要再这里释放
  ps.Free;
end;

{ TPerson }

procedure TPerson.Setage(const Value: Integer);
begin
  Fage := Value;
end;

procedure TPerson.Setname(const Value: string);
begin
  Fname := Value;
end;

procedure TPerson.Setsex(const Value: Boolean);
begin
  Fsex := Value;
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
end;

end.
原文地址:https://www.cnblogs.com/del88/p/6667724.html