type TMyClass = class(被繼承的類別) //若class後面不寫,則是繼承TObject(class的老祖宗)
Button1: TButton; // 不寫,置放VCL 物件,此為published 區
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
... { private declarations here}
protected
... { protected declarations here }
public
... { public declarations here }
published
... { published declarations here }
end;
public --- 不管同一單元或不同單元皆可存取,只要有引用皆可存取。
private --- 僅限於同一單元的可以存取,此點與 C++不同,在 C++ 中 private 的存取須同一個 class。
protected --- 僅限於繼承父類別的子子孫孫,方可以存取,不限制是否在同一個單元。
published --- 除了具有 Run-time Type Information (RTTI)的功能外,與 public 的功能完全相同。
RTTI 為了增加整個程式的生產力的東西,就像是設計階段已把元件屬性改掉,但執行階段想把剛設定的屬性值,靠RTTI幫你載入剛設定屬性的檔案。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tmyclass = class (Tobject) // 或 Tmyclass = class 自動繼承了Tobject
private
a: integer;
b: string;
public
c: integer;
procedure showData();
property pa: integer write a; //權限管理 pa虛擬變數可以寫入a
property pb: string read b; //權限管理 pb虛擬變數可以讀取b
property pc: string read b write b; //權限管理 pc虛擬變數可以讀取b寫入b
end;
var
Form1: TForm1;
myObj: Tmyclass; //定義在此,別的unit也可以讀得到,但若建立在implementation別的unit就讀不到囉
implementation
{$R *.dfm}
procedure Tmyclass.showData();
begin
showmessage(b);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
myObj := Tmyclass.create; //非視覺化的物件,記得要自己把它建立物件實體
myObj.pa := 12; // OK
//myObj.pb := 'ab'; // 不行,因為 readonly
showmessage( myObj.pb ); // OK因為可讀
myObj.pc:='12'; // OK,因可讀可寫
showmessage(myObj.pc); // OK
//myObj.a:=12; //private變數,在相同的unit下可讀取,
//myObj.b:='abc'; //不同的unit下不可讀取,可透過public 區的property變數pc來讀取
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
myObj.Free; //若物件有被建立則移除掉,若沒有,則不做動作,用此方式比較好
myObj.Destroy; //暴力移除掉物件,可能會有問題
end;
end.
第一步: 設計類別 ( 例子中沒有視覺化物件 )
類別內的成員有三種:(1) Field (2) Method (Function或 Procedure) (3) Property
Field: 就是定義在 class 內的變數
Method: 定義在 class 內的 procedure 或 function
Property: 針對現有 Field 增加存取的屬性
第二步:建立物件,依據類別所建立的實體
成員的存取
類別內 private 區的資料在同一個 unit 內的函數皆可存取,但在不同的 unit 內則不能存取。
(就像 implementation區內的資料)
類別內 pubilc 區的資料在同一個或不同的 unit 內的函數,只要有uses該單元皆可存取。
(像 interface 區內的資料)
記得物件本身的存取仍受到位於 interface 區或 implemenation 區的規範。
建構子 constructor 及解構子 destructor //像是unit的initialization
建構子宣告方式 constructor create; //像是unit的finalization
解構子宣告方式 destructor destroy;
以下為完整的範例,先建立一個Form使用兩個Buttons
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type Tc=class
public
a: integer;
constructor create;
destructor destroy;
private
end;
var
Form1: TForm1;
obj: Tc;
s: string;
implementation
{$R *.dfm}
constructor Tc.create; //當物件被create就會執行
begin
s:='object created';
end;
destructor Tc.destroy;
begin
s:='object destroyed';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
obj:= Tc.create;
showmessage(s);end;
procedure TForm1.Button2Click(Sender: TObject);
begin
obj.destroy;
showmessage(s);
end;
end.
沒有留言:
張貼留言