Introduction to interface programming based on Delphi

Source: Internet
Author: User
Tags definition ticket

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.