delphi接口基本学习摘录
生活随笔
收集整理的這篇文章主要介紹了
delphi接口基本学习摘录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
delphi接口基本學習摘錄
聲明接口
??????? IMyInterface = interface(IInterface)?
???????? ['{63E072DF-B81E-4734-B3CB-3C23C7FDA8EA}']?
????????????? function GetName(const str: String): String; stdcall;
????????????? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
????????????? function _AddRef: Integer; stdcall;? //使接口引用數加1。
????????????? function _Release: Integer; stdcall;? //使接口引用數減1,當小于等于0時作釋放動作。
??????? end;
???????????
說明:如果有繼續關系則在括號里填父接口,否則省卻,如:IMyInterface = interface這樣就行。
說明:GUID可選,如要實現具有COM特性的接口則需要加上
???????????? zc:接口GUID,最好加上,最好分離功能及其封裝。
接口中所有的數據成員都是public訪問限制,數據成員指定其為不能私有或其他的域成員。
接口中的方法只能有聲明,看上去像沒有構造和析構方法的純虛類。
type
? IInterface = interface
??? ['']
??? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
??? function _AddRef: Integer; stdcall;
??? function _Release: Integer; stdcall;
end;
? 該接口實現了QueryInterface,_AddRef,_Release,凡是繼承于該接口,則不需要聲明
該三個虛函數。
? 在實現新接口的類中,也要從TInterfacedObject 后繼承,原因在于該類實現以上函數的
實現部分,不要考慮QueryInterface等完成。如下:
type
? TNewInterfaceClass = class(TInterfacedObject, INewInterface)
?? ...
end;
接口的實現
???????? 接口服務是由類來實現的。
??????????????? TIntfClass = class(TObject, IMyInterface)
??????????????? private
??????????????????? FCounter: Integer;
??????????????????? FRefCount: Integer;
??????????????? public
??????????????????? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
??????????????????? ...
??????????????? end;
獲取接口
??? a. 使用類型轉換。
??????? 如:var aIntf: IMyInterface;
?????????????? begin
?????????????????????? aObj := TIntfClass.Create;
??????????????????????? try
??????????????????????????? aIntf := (IMyInterface(aObj);
??????????????????????????????? ...
??? b. 利用Delphi編譯器內建機制。 如:aIntf := aObj。
??? c. 利用對象的QueryInterface方法。
?????????? 如
???????????????? OleCheck(aObj.QueryInterface(IID, aIntf)); 只能存取有GUID的COM接口。
?? d. 利用as操作符。
???????? 使用as操作符必須符合下面條件:
??? 1.接口要從IInterface接口繼承下來。
??? 2.有GUID值
????? 在Delphi7中接口的實現類還必須是從TInterfacedObject繼承下來才行,
????? 如? TIntfClass = class(TInterfacedObject, IMyInterface)
?????? zc: 最好用該方法,考慮使用接口有其他語言開發l情況下的使用。
接口和對象生命期????????????
? 對接口直接=nil即可,freeAndNil則不需要。因接口特性,會計數減少1
接口的委托(Interface Delegation)
分為兩種:
1. 對象接口委托???
2. 類對象委托。
(略)
聲明接口
??????? IMyInterface = interface(IInterface)?
???????? ['{63E072DF-B81E-4734-B3CB-3C23C7FDA8EA}']?
????????????? function GetName(const str: String): String; stdcall;
????????????? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
????????????? function _AddRef: Integer; stdcall;? //使接口引用數加1。
????????????? function _Release: Integer; stdcall;? //使接口引用數減1,當小于等于0時作釋放動作。
??????? end;
???????????
說明:如果有繼續關系則在括號里填父接口,否則省卻,如:IMyInterface = interface這樣就行。
說明:GUID可選,如要實現具有COM特性的接口則需要加上
???????????? zc:接口GUID,最好加上,最好分離功能及其封裝。
接口中所有的數據成員都是public訪問限制,數據成員指定其為不能私有或其他的域成員。
接口中的方法只能有聲明,看上去像沒有構造和析構方法的純虛類。
type
? IInterface = interface
??? ['']
??? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
??? function _AddRef: Integer; stdcall;
??? function _Release: Integer; stdcall;
end;
? 該接口實現了QueryInterface,_AddRef,_Release,凡是繼承于該接口,則不需要聲明
該三個虛函數。
? 在實現新接口的類中,也要從TInterfacedObject 后繼承,原因在于該類實現以上函數的
實現部分,不要考慮QueryInterface等完成。如下:
type
? TNewInterfaceClass = class(TInterfacedObject, INewInterface)
?? ...
end;
接口的實現
???????? 接口服務是由類來實現的。
??????????????? TIntfClass = class(TObject, IMyInterface)
??????????????? private
??????????????????? FCounter: Integer;
??????????????????? FRefCount: Integer;
??????????????? public
??????????????????? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
??????????????????? ...
??????????????? end;
獲取接口
??? a. 使用類型轉換。
??????? 如:var aIntf: IMyInterface;
?????????????? begin
?????????????????????? aObj := TIntfClass.Create;
??????????????????????? try
??????????????????????????? aIntf := (IMyInterface(aObj);
??????????????????????????????? ...
??? b. 利用Delphi編譯器內建機制。 如:aIntf := aObj。
??? c. 利用對象的QueryInterface方法。
?????????? 如
???????????????? OleCheck(aObj.QueryInterface(IID, aIntf)); 只能存取有GUID的COM接口。
?? d. 利用as操作符。
???????? 使用as操作符必須符合下面條件:
??? 1.接口要從IInterface接口繼承下來。
??? 2.有GUID值
????? 在Delphi7中接口的實現類還必須是從TInterfacedObject繼承下來才行,
????? 如? TIntfClass = class(TInterfacedObject, IMyInterface)
?????? zc: 最好用該方法,考慮使用接口有其他語言開發l情況下的使用。
接口和對象生命期????????????
? 對接口直接=nil即可,freeAndNil則不需要。因接口特性,會計數減少1
接口的委托(Interface Delegation)
分為兩種:
1. 對象接口委托???
2. 類對象委托。
(略)
總結
以上是生活随笔為你收集整理的delphi接口基本学习摘录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql server 判断是否存在数据库
- 下一篇: 点评老师freeeim