delphi7中類的定義和使用

來源:互聯網
上載者:User

更多參考《delphi7完美經典》第七章。

在delphi7中怎麼建立一個自己的類:

1、類定義位置:建立一個單元,如下所示

unit Unit3;interfaceimplementationend.

類的定義應在interface和implementation之間。類的實現部分在implementation和end.之間。以type後類定義始以end;止。

2、類成員類型:public、private、published預設類型為public

3、建構函式、解構函式、重載函數。

建構函式和解構函式必須是固定格式。重載函數通過在函數定義後加overload;實現。

4、類成員資料和成員方法的位置關係:在成員類型關鍵字之後必須是先成員資料後成員方法,否則會報錯:Field definition not allowed after methods or properties.

5、類的使用:聲明一個類對象後,必須先調用其create方法,之後才可使用該類對象。

6、free是TObject類的方法,never call destroy directly,而是調用free
method。

有兩個比較nb的事:

1)僅有成員函式宣告沒有成員函數實現會報錯。

2)delphi的initialization和finalization單元,可代替構築函數和解構函式。

類定義和使用樣本

unit Unit2;interface//有基類時 type TStudent = class(基類)type TStudent=class  procedure SetID(ID:string);public  ID : string;  function GetID:string;  constructor Create(i:integer); overload;  constructor Create ; overload;  destructor Destroy;end;implementation  procedure TStudent.SetID(ID:string);beginend;function TStudent.GetID:string;begin  result := ID;end;constructor TStudent.Create(i:integer);begin  ID := 'abc';end;constructor TStudent.Create;beginend;destructor TStudent.Destroy;beginend;end.

procedure TForm1.Button1Click(Sender: TObject);var  stuA : TStudent;begin  stuA := TStudent.Create(1);  ShowMessage(stuA.GetID);end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.