Delphi的介面(2)

來源:互聯網
上載者:User

第一個例子的操作實況錄影:

http://www.bianceng.net/delphi/201212/657.htm

代碼檔案:

unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls; type  TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Button3: TButton;    Button4: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button4Click(Sender: TObject);  end;   IMyInterface1 = interface    function Func1: Integer;    function Func2: Integer;  end;   IMyInterface2 = interface    procedure Proc1;    procedure Proc2;  end;   TMyClass1 = class(TInterfacedObject, IMyInterface1, IMyInterface2)  public    procedure Proc1;    procedure Proc2;    function Func1: Integer;    function Func2: Integer;  end; var  Form1: TForm1; implementation {$R *.dfm} { TMyClass1 } function TMyClass1.Func1: Integer;begin  ShowMessage('IMyInterface1.Func1');  Result := 0;end; function TMyClass1.Func2: Integer;begin  ShowMessage('IMyInterface1.Func2');  Result := 0;end; procedure TMyClass1.Proc1;begin  ShowMessage('IMyInterface2.Proc1');end; procedure TMyClass1.Proc2;begin  ShowMessage('IMyInterface2.Proc2');end; procedure TForm1.Button1Click(Sender: TObject);var  c: TMyClass1;begin  c := TMyClass1.Create;   c.Func1;  c.Func2;   c.Proc1;  c.Proc2;   c.Free;end; procedure TForm1.Button2Click(Sender: TObject);var  i1: IMyInterface1;begin  i1 := TMyClass1.Create;  i1.Func1;  i1.Func2;end; procedure TForm1.Button3Click(Sender: TObject);var  i2: IMyInterface2;begin  i2 := TMyClass1.Create;  i2.Proc1;  i2.Proc2;end; procedure TForm1.Button4Click(Sender: TObject);var  c: TMyClass1;  i1: IMyInterface1;  i2: IMyInterface2;begin  c := TMyClass1.Create;   i1 := c;  i1.Func1;  i1.Func2;   i2 := c;  i2.Proc1;  i2.Proc2; //  c.Free; {}end; end.

樣本注釋(現在應該知道的):

{  1、介面命名規範 I 起頭, 就像類從 T 打頭一樣.   2、介面都是從 IInterface 繼承而來; 若是從根介面繼承, 可省略.   3、介面成員只能是方法、屬性, 沒有欄位.   4、介面成員都是公開的, 不需要 private、protected、public、published 等任何訪問限制.   5、因為介面只聲明、無實現, 也用不到繼承與覆蓋相關的修飾(virtual、dynamic、abstract、override).   6、一個介面可以從另一個介面繼承, 但不能從多個介面繼承; 不過 Delphi.Net 已支援介面的多繼承了.   7、一個類可以實現多個介面: TMyClass = class(父類, 介面1, 介面2, ...)  end;   8、不過實現介面的類有多麼豐富, 介面只擁有自己聲明的成員.   9、實現介面的類一般繼承於 TInterfacedObject, 直接從 TObject 繼承會增加一些麻煩而重複的工作.   10、介面在用完後會自釋放, 並同時釋放擁有它的類; 這很方便, 但同時帶來很多問題.} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.