首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 屬性機制完整指南:Property、Read/Write、存取方法與封裝實戰

     編輯:Delphi

Delphi 的 property 是封裝的核心語法,將資料存取與業務邏輯解耦,支援讀寫方法、索引、默認值、存儲控制等豐富特性。本文通過一個完整的 TPerson 類別範例,詳細講解屬性聲明、讀寫方法、直接字段存取、陣列屬性、類屬性等用法。

基本屬性聲明

unit Person;

interface

type
  TPerson = class(TObject)
  private
    FName: string;
    FAge: Integer;
  public
    procedure SetName(const StrName: string);
    procedure SetAge(const IntAge: Integer);
    property Name: string read FName write SetName;
    property Age: Integer read FAge write SetAge;
  end;

implementation

procedure TPerson.SetName(const StrName: string);
begin
  FName := StrName;
end;

procedure TPerson.SetAge(const IntAge: Integer);
begin
  if IntAge < 0 then FAge := 0 else FAge := IntAge;  { 業務驗證 }
end;

end.

使用範例

uses Person;

procedure TForm1.Button1Click(Sender: TObject);
var
  PersonOne: TPerson;
begin
  PersonOne := TPerson.Create;
  try
    PersonOne.Name := '萬一';      { 呼叫 SetName }
    PersonOne.Age := 100;          { 呼叫 SetAge }
    ShowMessage('姓名:' + PersonOne.Name + '; 年齡:' + IntToStr(PersonOne.Age));
    { 讀取直接存取 FName/FAge }
  finally
    PersonOne.Free;
  end;
end;

屬性完整語法

property PropertyName[IndexList]: Type
  read FieldOrMethod
  write FieldOrMethod
  stored True/False/Method
  default Value
  nodefault
  index IntegerConstant
  implements Interface;

關鍵字說明

關鍵字用途範例
read指定讀取來源:字段或無參函數read FName / read GetName
write指定寫入目標:字段或單參過程write FAge / write SetAge
stored序列化時是否存儲(DFM/流)stored False / stored IsNameStored
default指定默認值(僅序列化優化)default 0
nodefault明確無默認值
index多屬性共用同一存取方法時區分property Items[I]: Integer read GetItem write SetItem index 1;
implements委托介面實現property MyIntf: IMyIntf read FIntf implements IMyIntf;

進階屬性模式

1. 陣列屬性(索引器)

type
  TIntList = class
  private
    FData: array of Integer;
    function GetItem(Index: Integer): Integer;
    procedure SetItem(Index: Integer; Value: Integer);
  public
    property Items[Index: Integer]: Integer read GetItem write SetItem; default;
    property Count: Integer read GetCount;
  end;

var
  L: TIntList;
begin
  L := TIntList.Create;
  L.Items[0] := 10;  { 呼叫 SetItem(0, 10) }
  ShowMessage(IntToStr(L.Items[0])); { 呼叫 GetItem(0) }
  L.Free;
end;

2. 類屬性

type
  TConfig = class
  strict private
    class var FTimeout: Integer;
    class function GetTimeout: Integer; static;
    class procedure SetTimeout(Value: Integer); static;
  public
    class property Timeout: Integer read GetTimeout write SetTimeout;
  end;

begin
  TConfig.Timeout := 30;  { 無需實例 }
end;

3. 唯讀/唯寫屬性

property ReadOnlyProp: string read FValue;      { 唯讀 }
property WriteOnlyProp: Integer write SetValue; { 唯寫 }

屬性 vs 公開字段

特性公開字段屬性
封裝驗證無寫入方法可驗證/轉換
唯讀控制無省略 write
二進位兼容修改類型破壞兼容內部實現可變,外部介面不變
RTTI 可見性僅 published 字段published 屬性完整支援
介面實現不可implements 委托

最佳實踐清單

  1. 所有對外可見資料成員統一用 property,私有字段前綴 F(FName)。
  2. 簡單屬性直接 read FField write FField,需驗證/副作用時用方法。
  3. stored False 用於運行時計算屬性、緩存值,避免 DFM 膨脹。
  4. default 僅用於序列化優化,不影響運行時初始值(需在建構子初始化)。
  5. 陣列屬性標記 default 可讓 Obj[0] 簡寫為 Obj.Items[0]。

屬性是 Delphi 封裝的基石,善用讀寫方法、索引、默認值與 RTTI 特性,能構建既安全又靈活的類別介面。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved