Delphi interface [reprinted] Delphi object-oriented Learning Article 6: Interface

Source: Internet
Author: User
Tags uppercase letter
Delphi object-oriented Learning Article 6: Interfaces
Author: banhamt
(Please indicate the source and keep it complete)

In object culture, class inheritance is a very powerful mechanism, and a more powerful Inheritance Mechanism should be inherited from an interface.
In this article, we will discuss the features of interfaces.
First, the interface is defined in a similar way as a class. The difference is that a class represents an entity, and an interface represents a batch of Operation Specifications. In addition, all data members in the interface are restricted by public access. That is to say, you cannot specify the data member in the interface as a private or other domain member. In addition, methods in the interface can only be declared but cannot be implemented, so it looks more like a pure virtual class without constructor and constructor methods.
Many of the materials I have read refer to "Multi-inheritance" when introducing interfaces ", it seems that the interface is only designed to make up that Object Pascal does not support multi-inheritance (at least the first impression on me). In fact, the interface is very powerful, it is also an indispensable part of object-oriented programming.
The reason why an interface is powerful is that the interface only needs to tell the user what the method name is and what parameters it has. It does not need to care about how the method is implemented. For example, the construction and work style of a computer is not important to the general user, because the general user is more concerned about how to use it. Therefore, the computer interface, such as the mouse, keyboard, and display, is the most important thing for users. This provides us with great convenience to achieve the "separation", the core concept of objectization.

First, let's take a look at the interface definition method: The following is the famous method of the iinterface interface in system. PAS in Delphi.

Type
Iinterface = interface
['']
Function QueryInterface (const IID: tguid; out OBJ): hresult; stdcall;
Function _ addref: integer; stdcall;
Function _ release: integer; stdcall;
End;

We can see that it is similar to the basic declaration of the class, but it is changed from the keyword class to the interface.
You may notice the [''] following the declaration. What is this? This is actually the unique identifier of the interface, that is, the tguid we call. After the interface is registered to the system, we can retrieve key values such as 1270000-0000-c000-000000000046 through the registry. This means that we only need to know the value of a tguid to access this interface conveniently.
Of course, you can define other methods in the interface, But Delphi does not allow adding variable members to the interface, because the interface does not allow implementations.

Similarly, interfaces can inherit, encapsulate, and overwrite methods. Similar inheritance of inheritance interfaces:

Type
Inewinterface = interface (iinterface)
// Define a new interface inewinterface and tell the compiler that it inherits from the iinterface Interface
....
End;

Note that the tguid of an interface is the unique identifier of each interface. In other words, the tguid cannot be repeated. You cannot simply copy it from another place. It is wrong. If you need a tguid, you can press Ctrl + Shift + G in the Delphi code editing box to obtain a new tguid value, which is automatically generated by Delphi.

The inheritance of interfaces is roughly like this. Like tobject is the root class of all classes, in Delphi, iinterface is the root class of all interfaces, which is similar to the following inheritance:

Type
Inewinterface = interface
...
End;

In fact, it also defines a new interface that inherits from iinterface inewinterface (just like adding an uppercase letter T before the class name, we are used to adding an uppercase letter I before the Interface Name, of course, this is just a naming convention)

As we have said, interfaces can only be declared and cannot be implemented. So how can we make the interface work for us?
In fact, the implementation of interfaces needs to be completed by means of classes (of course, this seems like writing multiple inheritance statements in C ++). Note that since interfaces need to be implemented by using classes, that is to say, the class used to implement the interface must implement all the methods defined in the interface:

Type
Tnewinterfaceclass = Class (tinterfacedobject, inewinterface)
// Tinterfacedobject is the class name, And inewinterface is the interface name defined above
...
End;

Generally, the base class we use to implement the interface does not select tobject but tinterfacedobject. The reason is that the tinterfacedobject class has helped us implement methods in the iinterface interface, we only need to implement the new methods in our own interface.

Since we have implemented methods in the interface through the class, we can use this interface to serve us, And the instantiation interface is also very simple:

VaR
Newface: inewinterface;
Begin
Newface: = tnewinterfaceclass. Create (); // create an interface
Newface. xxx; // call the method in the interface
Newface: = nil; // release
End;

Some may wonder why NIL is directly assigned to the API release? As we have mentioned earlier, the interface has no constructor or destructor. Since there is no destructor, it means that we cannot release interfaces by releasing classes.
Is memory leakage caused by null interface objects?
The answer is no, because the interface provides a reference counting mechanism: When an interface instance (that is, an object implementing this interface) is referenced, for example, when an interface variable is assigned, the addref method of the interface instance is automatically called by the compiler, and the reference count is increased by one. When the reference is canceled, the compiler calls the _ release method to reduce the reference count by one. When the reference count is reduced to zero, it indicates that no other interface variable has referenced this interface instance, and the compiler will release it automatically.

Note that interface objects and class objects cannot be mixed. Of course, in the above example, newface can only call the methods defined in the inewinterface interface, but cannot call methods defined in the class that do not exist in the interface.

Of course, a class can implement multiple interfaces at the same time. Multiple Interfaces are separated by commas. For example:

Type
Tnewinterfaceclass = Class (tinterfacedobject, iinterface1, iinterface2)
...
End;

Similarly, classes that implement multiple interfaces must implement the methods defined in each interface in sequence.
So there is a problem -- What if two interfaces have methods with the same name? It's easy to get them aliases:

Type
Iinterface1 = interface (iinterface)
// Interface 1
Fucntion func (): Boolean;
End;

Iinterface2 = interface (iinterface)
// Interface 2
Function func (): Boolean;
End;

Tclasses = Class (tinterfacedobject, iinterface1, iinterface2)
Public
Function iinterface1.func: func1;
Function iinterface2.func: func2;
{Alias for method with the same name}
Function func1: Boolean;
Function func2: Boolean;
{Declaration method}
End;

In Delphi, The imploements indicator can also be used to delegate another class or interface to implement a method of the interface. Sometimes this method is also called delegate implementation. The implements usage is as follows:

Type
Tinterclass = Class (tinterfacedobject, iinterface1)
...
Function getclasses: tclasses;
Property face: tclasses read getclasses implements iinterface1;
...
End;

In the above Code, the implements indicator requires the compiler to find methods to implement the iinterface1 interface in the face attribute. The attribute type must be a class or an interface. Implements can specify multiple interfaces, which are separated by commas.

The advantages of implements are:
1. It allows interface aggregation without conflict. (Aggregation is a concept in COM)
2. He can delay using the resources required for implementing interfaces until resources are required.

The following is a detailed example of delegation (this example passes debugging in Delphi7 + Win2000 SP4 ):

Inewinterface = interface (iinterface)
// Define the interface
Function sayhello: string; stdcall;
// Interface Method
End;

Tnewclass = Class (tinterfacedobject, inewinterface)
Public
Function sayhello: string; stdcall;
// Method in the first class implementation Interface
End;

Tnewclass1 = Class (tinterfacedobject, inewinterface)
Private
Fnewclass: inewinterface;
Public
// Note that the syahello method in the interface is not implemented in this class.
Constructor create;
Destructor destroy; override;
Property newclass: inewinterface read fnewclass implements inewinterface;
// Interface object delegate if it is a Class Object delegate
// Property newclass: tnewclass read fnewclass implements inewinterface;
End;

Implementation

Function tnewclass. sayhello: string;
Begin
Result: = classname;
Showmessage (result );
End;

Constructor tnewclass1.create;
Begin
Inherited create ();
Fnewclass: = tnewclass. Create;
// Create an interface in the constructor
End;

Destructor tnewclass1.destroy;
Begin
Fnewclass: = nil;
// Release the interface in the destructor
Inherited destroy ();
End;

Call example:

VaR
Newinterface: inewinterface;
Begin
Newinterface: = tnewclass1.create;
Newinterface. sayhello;
Newinterface: = nil;
End;

Off-topic: If you haven't touched COM/COM +, you may think the interface is very troublesome (PS: when I first learned this year, I really wanted to kill people who invented the interface mechanism.) But after the interface is encapsulated by COM, it will become very meaningful!

 

Completely reload!

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.