随笔 - 809  文章 - 0 评论 - 144 阅读 - 770万

 

 

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.
复制代码

 

posted on   del88  阅读(284)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2012-04-05 DBGridEh,DBGrid点击标题排序
2012-04-05 DBGridEH点击标题自动排序的设定及排序标志相反BUG的修正(转) --根本就没有bug 没事找事
2012-04-05 dbgrideh 标题排序(自己总结1)
点击右上角即可分享
微信分享提示