理解 Delphi 的類(二) – 初識類的方法

來源:互聯網
上載者:User
說到"類", 就會提到:
屬性方法事件 (這是類包含的內容);
封裝繼承多態 (這是類的主要用途).

下面定義並調用了了一個過程 MyProc、一個函數 MyFun.

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}//這是一個過程, 過程沒有傳回值procedure MyProc(var x: Integer);begin  x := x * 2;end;//這是一個函數, 函數必須有傳回值function MyFun(var x: Integer): Integer;begin  x := x * 2;  Result := x;end;//調用過程與函數procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  i := 6;  MyProc(i);                {使用過程}  ShowMessage(IntToStr(i)); {12}  i := 6;  i := MyFun(i);            {使用函數}  ShowMessage(IntToStr(i)); {12}end;end.

把過程與函數 MyProc、MyFun 包含在一個類裡面.

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}Type  TMyClass = class    procedure MyProc(var x: Integer);    function  MyFun (var x: Integer): Integer;  end;{  上面定義了一個類, 其中包含了兩個方法;  函數與過程來到類裡面一般叫做方法, 函數是有傳回值的方法、過程是沒有傳回值的方法;  在這裡, 方法只有定義沒有實現;  但必須在本單元的 implementation 區內實現.  下面就是兩個方法的實現:}function TMyClass.MyFun(var x: Integer): Integer;begin  x := x * 2;  Result := x;end;procedure TMyClass.MyProc(var x: Integer);begin  x := x * 2;end;//調用測試procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;  myClass: TMyClass;          {對象聲明}begin  myClass := TMyClass.Create; {對象建立}  i := 6;  myClass.MyProc(i);          {調用方法}  ShowMessage(IntToStr(i));   {12}  i := 6;  i := myClass.MyFun(i);      {調用方法}  ShowMessage(IntToStr(i));   {12}  myClass.Free;               {對象釋放}end;end.

一般情況下, 類都會定義在 interface 區; 在 implementation 區定義的類只能本單元使用.

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;  {類定義, 因為已經在 Type 區了, 可以省略 Type 關鍵字}  TMyClass = class    procedure MyProc(var x: Integer);    function  MyFun (var x: Integer): Integer;  end;var  Form1: TForm1;implementation{$R *.dfm}{TMyClass 中方法的實現}function TMyClass.MyFun(var x: Integer): Integer;begin  x := x * 2;  Result := x;end;procedure TMyClass.MyProc(var x: Integer);begin  x := x * 2;end;//調用測試procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;  myClass: TMyClass;          {對象聲明}begin  myClass := TMyClass.Create; {對象建立}  i := 6;  myClass.MyProc(i);          {調用方法}  ShowMessage(IntToStr(i));   {12}  i := 6;  i := myClass.MyFun(i);      {調用方法}  ShowMessage(IntToStr(i));   {12}  myClass.Free;               {對象釋放}end;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.