Why use an interface?
For example: there is such a ticket service, cinemas can sell tickets, the Opera House can sell tickets, the passenger station can also sell tickets, then we need to the cinema, opera and passenger stations are designed into a class structure to provide ticket sales services? You know, even managers can sell tickets, it is obviously not suitable for the manager also included in the inheritance structure of ticket services, we need only a common ticket sales service. As a result, the ticket service is an interface, cinema, opera, etc. as long as you follow such a service definition can be very good interaction and communication (if required).
How to use the interface in Delphi
1, the Declaration interface
IMyInterface = interface(IInterface) //说明(1)
['{63E072DF-B81E-4734-B3CB-3C23C7FDA8EA}'] //说明(2)
function GetName(const str: String): String; stdcall;
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall; //说明(3)
function _AddRef: Integer; stdcall; //使接口引用数加1。
function _Release: Integer; stdcall;//使接口引用数减1,当小于等于0时作释放动作。
end;
Description (1): If there is a continuation of the relationship between the parentheses to fill in the parent interface, otherwise save, such as: IMyInterface = interface so on the line.
Description (2): This GUID is optional, if you want to implement the interface with COM characteristics, you need to add, Delphi for the interface with a GUID at runtime in the VMT table at the scheduled location of the information generated interface, such as the definition of interface methods, method parameter definition can be detailed information.
Description (3): The interface must implement these three functions.
2, the implementation of the interface
Interface services are implemented by classes.
TIntfClass = class(TObject, IMyInterface)
private
FCounter: Integer;
FRefCount: Integer;
public
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
...
end;