Property プロパティ
Delphi でクラスにプロパティを追加するのは VB での方法とは大違いです。
プロパティが変数と直接結びついている場合は以下のように記述します。
| ■ プロパティ宣言の例 |
type
TTestClass = class(TObject)
private
FKeyword: string;
public
//Keywordプロパティ
property Keyword : string read FKeyword write FKeyword;
end;
|
この場合、プロパティ "Keyword" は読み書き可能です。
これを読みとり専用プロパティにするには、read のみにします。
| ■ 読みとり専用プロパティ宣言の例 |
type
TTestClass = class(TObject)
private
FKeyword: string;
public
//Keywordプロパティ
property Keyword : string read FKeyword;
end;
|
読みとり、書き込みを関数や手続きに割り当てたい場合は read 、 write の後ろに変数ではなく関数、手続きを記述します。
| ■ 関数、手続きを経由するプロパティの例 |
type
TTestClass = class(TObject)
private
FKeyword: string;
function GetKeyword: string;
procedure SetKeyword(const Value: string);
public
//Keywordプロパティ
property Keyword : string read GetKeyword write SetKeyword;
end;
implementation
{ TTestClass }
//Keywordプロパティ (GET)
function TTestClass.GetKeyword: string;
begin
Result := FKeyword;
end;
//Keywordプロパティ (SET)
procedure TTestClass.SetKeyword(const Value: string);
begin
FKeyword := Value;
end;
|
もちろん混在も可能です。
| ■ 関数、手続きを経由するプロパティの例 |
type
TTestClass = class(TObject)
private
FKeyword: string;
procedure SetKeyword(const Value: string);
public
//Keywordプロパティ
property Keyword : string read FKeyword write SetKeyword;
end;
|