プロパティは、クラスの中で定義したフィールドを変更するためのフィールドのようなものと考えると分かりやすいかもしれません。実際、オブジェクトインスペクタで表示されている「プロパティ」は、ここで扱うプロパティのことです。
プロパティを使うには、まず「宣言」をしなければなりません。これは、キーワード property を使います。次に、プロパティの名前、プロパティの型、プロパティの値を読み書き( read, write ) を宣言します。まず最初に、簡単な例をみてみましょう。
TBook = class(TObject) private FName: string; FSize: Integer; public property Name: string read FName write FName; property Size: Integer read FSize; end;
プロパティにアクセスする場合は、read、write を使用します。上の例では、プロパティ Name ( 型は string ) は、read( 読み ) 、write( 書き ) がありますので、「読み書き」の両方ができ、プロパティ Size ( 型は Integer ) は、read しかありませんので、「読み」専用です。
また、上の例のように read, write に直接フィールドを設定している場合のことを「直接アクセス」と呼ばれています。まずは、この直接アクセスを使った例をみてみます。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
TBook = class(TObject)
private
FName: string;
FSize: Integer;
public
constructor Create;
property Name: string read FName write FName;
property Size: Integer read FSize;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TBook }
constructor TBook.Create;
begin
FName := '我輩は猫である';
FSize := 100;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Book: TBook;
s: string;
i: Integer;
begin
Book := TBook.Create;
s := Book.Name;
Label1.Caption := s;
i := Book.Size;
Label2.Caption := IntToStr(i);
Book.Name := '坊ちゃん';
Label3.Caption := Book.Name;
Book.Size := 200; // 読み込み専用プロパティには、書き込み出来ません!
Book.Free;
end;
end.
使い方は単に、(オブジェクト).(プロパティ) のようにするだけで、プロパティにアクセスする事ができます。それでは次にメソッドを用いた例です。メソッドを用いても基本的には、直接アクセスとそれ程、使い方自体は変わりません。メソッドを用いた場合と直接アクセスとの違いの一つに、コンポーネントのプロパティをオブジェクトインスペクタで変更する場合がありますが、ここでは、あまり関係ありません。
TBook = class(TObject) private FBookArray: array[0..2] of string; public property BookArray[index: Integer]: string read GetBookArray write SetBookArray; end;
このようにメソッドを使用する場合は read の場合には、そのメソッドの名前に Get をつけ GetBookArray のようにします。 write の場合は、Set をつけ、SetBookArray のようにします。そして、使い方なのですが、Delphi のバージョンによって「クラス定義補完」という機能を使う事が出来ます。( Delphi6 personal では使う事が出来ません。) この「クラス定義補完」を使うには、プロパティの上にカーソル(マウスカーソルではありません)を置いて「右クリック」→「クラス定義補完」か或いは、「Ctrl + Shift + C」を押すと実行でき、以下のように自動的にコード補完を行ってくれます。
TBook = class(TObject)
private
FBookArray: array[0..2] of string;
function GetBookArray(index: Integer): string;
procedure SetBookArray(index: Integer; const Value: string);
public
property BookArray[index: Integer]: string
read GetBookArray write SetBookArray;
end;
...............................
function TBook.GetBookArray(index: Integer): string;
begin
end;
procedure TBook.SetBookArray(index: Integer; const Value: string);
begin
end;
「クラス定義補完」は、Delphi6 personal には、ありませんのでこれは手書きする必要があります。使い方は、以下のようになります。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
TBook = class(TObject)
private
FBookArray: array[0..2] of string;
function GetBookArray(index: Integer): string;
procedure SetBookArray(index: Integer; const Value: string);
public
constructor Create;
property BookArray[index: Integer]: string
read GetBookArray write SetBookArray;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TBook }
constructor TBook.Create;
begin
FBookArray[0] := 'ブラックジャック';
FBookArray[1] := '火の鳥';
FBookArray[2] := 'どろろ';
end;
function TBook.GetBookArray(index: Integer): string;
begin
Result := FBookArray[index];
end;
procedure TBook.SetBookArray(index: Integer; const Value: string);
begin
FBookArray[index] := Value;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Book: TBook;
s: string;
begin
Book := TBook.Create;
s := Book.BookArray[Random(3)];
Label1.Caption := s;
Book.BookArray[0] := '老人と海';
Label2.Caption := Book.BookArray[0];
Book.Free;
end;
end.
ここで使用した BookArray は「配列プロパティ」と呼ばれています。