{After understanding OOP, we explain the other knowledge points you have to have the components: interface, maybe as you just contact the interface, this is boring, but you must give you an explanation, the interface mentioned here Will be 100% reference to future component techniques or components objects because they are ultimately the implementation, packaging of the interface! Quote Previous article}
What is an interface? What is the role of the interface? How to use an interface?
A series of questions will be entangled with you. If you don't want to make components, distributed, local application calls, you don't have to read;
COM1 / COM2 and other hardware interfaces, we are unfamiliar; but if you want to put the interfaces in the software, do they? How to use the interface? And use it, it is not necessarily easy; let us continue;
Our so-called interface is actually some processes, functions, attribute sets; remember, the interface can not have a field, if you have this idea, then from now on, it is wrong, and the access to the interface is to provide it. The method, event, attribute access, and the method provided by the interface is public, which is all disclosed, so it is not necessary to use public;
In the component, the interface is everything, a component is an interface set, and the user only uses the interface talent component to deal;
The most universal interface
IUNKNOWN: Interface; // Default interface
First let's look at a simple routine and then conduct a detailed introduction;
Unit main;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Comobj, ActiveX, STDVCL;
Type
TFORM1 = Class (TFORM)
Button1: tbutton;
Procedure Button1Click (Sender: TOBJECT);
Procedure formcreate (Sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
END;
ILC = Interface (IUNKNOWN)
[{4FFE6DDB-80B9-4E2D-A05F-5F3B35311ED7} ']
// GUID, it is used to unique identifiers that identify an interface, can generate a set of GUIDs via Ctrl Shift G, and you can think that the GUID you produce is unique worldwide. Never worry that the GUID will be used.
Procedure setValue (NewValue: String);
Function getValue: String;
END;
TLC = Class (TinterFaceDObject, ILC)
public
Value: String;
Procedure setValue (NewValue: String);
Function getValue: String;
DESTRUCTOR DESTROY; OVERRIDE;
END;
VAR
FORM1: TFORM1;
IMYLC: ILC;
IMPLEMENTATION
{$ R * .dfm}
{TLC}
DESTRUCTOR TLC.DESTROY;
Begin
Application.MessageBox ('resource has been completely released', 'Operation Tips', MB_OK MB_ICONInformation);
inherited;
END;
Function TLC.GetValue: String;
Begin
Result: = Value;
ShowMessage (Result);
END;
Procedure TLC.SetValue (NewValue: String);
Begin
Value: = newValue;
END;
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Begin
Imylc.setValue ('first COM routine');
IMYLC.GetValue;
END;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Imylc: = tlc.create;
END;
End.
Interfaces interface
The interface defines a type containing a set of abstract methods. Why is it to include a set of abstract types? The reason is that the inheritance of the interface can complete the classes such as OverLoad (of course, there is no syntax, but the effect is exactly the same), which is also the special location of the interface, at the same time, the method in the interface is completely public, Just as public, but there is no need to add this keyword. A class, even from a simple base class inherits, you can implement any more interfaces. The interface is similar to the abstract class (ie there is no field and all methods are class of abstract methods), and Delphi provides additional features. Delphi interface Sometimes it is very like a COM (Component Object Model) interface, however, you can use Delphi to implement the interface of the component object model for us, but use Delphi interface does not need you to know about COM, you can also Use the interface as many other purposes.
Declare a new interface - it inherits in an existing interface. The default inherits in the iUnkown interface, the declaration of the interface contains the declarations of methods and properties, but there is no field. As all classes are inherited in TOBJECT, all interface classes are inherited from IunkNown. Interface IUNKNOWN defines three ways: _ADDREF, _RELEASE, and QueryInterface. If you are familiar with COM, it will not be unfamiliar. The first two methods are used to manage the lifecycle reference count of the object that implements this interface. The third method is used to access other interfaces that the object may be implemented. As for the reference count, it is a relatively live reference. In the OOP, many places reference the reference counting technology. You will see that by reference to counting functions, you will make some operations that violate the original intention of the component, such as advance release components, postpone the release assembly, and don't release the components, of course, I don't recommend this practice, unless you do this. You will see this practice in the routines behind you.
Declare that the interface is not equal to applying this interface, you must have a Coclass class (CoClass) to implement this class. If you are more familiar with Type Library, you should be very clear about this implementation, this is later, you It will see its usage in the OCX instance. Here, it emphasizes that there is no meaning of the interface that is not implemented. This is said that this is because the ultimate goal of an interface is to serve the user, but it can be applied to this interface (front side) I mentioned that the interface class such as an abstract class is also the case, and emphasizing that there is no field in the interface because it does not have the ability to store!);
Remind you, Delphi does not support more inheritance, but you can use interfaces to achieve more inheritance, and another one can achieve more inheritance through design patterns (not belonging to us), when you want to declare a realization Or the class of multiple interfaces, you must implement all the methods declared in the interface. The new class can directly implement the interface, or you can delegate this implementation to an attribute - its value is an interface. The easiest way to implement _addref, _release, and queryinterface method is to inherit TinterFaceDObject and its derived approach, of course, you can also inherit from other classes. If you want your own way. Such as: TVClass = Class (TinterFaceDObject, Ivinterface);
Understand: TVClass acts as an implementation class in the Ivinterface interface, which is inherited in TinterFaceDObject, which is implemented in IVInterface, and it can implement multiple interfaces.
New classes must be used in the method name, parameters, and calling conventions that are consistent with the interface method when implementing the interface. Delphi automatically pairs the method of the class with the corresponding method of the interface. Such as routines:
VAR
MyObject: TOBJECT;
MyNumber: iformattedNumber;
Begin
MyObject: = TFORMATTEDINTEGER.CREATE (12);
...
ifObject.getInterface (iformattedNumber, MyNumber) THEN
ShowMessage (MyNumber.FormattedString);
END;
{Description: An instance here extracted ourselves, detailed reference routines, iFormattedNumber is an interface, TFORMATTEDNUMBER is its coclass}
If you want to use different method names, you can use different method names to redirect the interface. The method used as a redirection must have the consistent parameters and call conventions for the method of the interface. This feature is very important, and when a class needs to implement multiple interfaces, it is especially true when there are repetitive methods. In this case, it is necessary to use an alias or use the Implements indicator to entrust the interface to an attribute. The value of this property must be the type of interface that this class will implement. When the object is mapped to the interface, Delphi automatically obtains the value of the attribute and returns the interface. These detail issues will not be described herein.
Reference country counting count
The object has a process of creation, release, and the interface is also, the component is even more, and the interface also has a life cycle. The life cycle of the interface is as determined by a count variable (FCount). When to release a component is a category of reference technology, (I hope that I will explain this) There are four ways to release this, which can be in the decision of its interface, itself, and component objects. Different times are released, and forced release interface: = NIL here, you now as long as you understand the interface, components' release and reference count variables have a big relationship.
The compiler triggers the call to _addref and _Release to manage the life cycle of the interface object. To use the Delphi's automatic reference count, declare a variable of an interface type. When you assign an interface reference to an interface variable, Delphi automatically calls _addref. Delphi automatically calls _RELEASE when changing the variable amount away.
_Addref and _Release behavior depends entirely on you. If you inherit from TinterFaceDObject, these methods complete the reference to the reference count. _Addref method is used to increase the reference count, _Release is used to reduce the reference count. When the reference count is 0, the _RELEASE method will release the object. If you inherit from other classes, you can define your own way. However, you should correct the queryinterface method correctly because Delphi is based on this to implement AS operations. For reference counting techniques, I will explain in different articles, and in different situations, _addref and _release operations, and interface transformation, etc., if you are interested, please pay attention to some articles I have written in the recent. Some simple metapings will be implemented below. {
Description: TinterFaceDObject implements iUnkown's _addref and release, and queryinterface, so the CoClass we declared as long as inheriting from it, otherwise you may need to write these three ways to implement the implementation method, however, this is not a decision. . It is only earlier than TinterFaceDObject proposed, so many materials are as base classes. In the example below, you will be able to see how to manually implement these three methods, and destroy the rules of the reference count.
}
Delphi calls QueryInterface to implement part of the AS operations on the interface. You can use the AS operator to convert an interface to another interface. Delphi calls QueryInterface to get a new interface reference. If QueryInterface returns an error, the AS operation will trigger a running period error. (This runs in the SYSUTILS unit are mapped to the EintFCastError exception class.)
You can use your own way to implement queryinterface methods, although you may tend to be close to the implementation of TinterFaceDObject. As shown below is a class that implements a normal QueryInterface method, but the implementation of _addref and _Release methods is indeed different. You will see what is used in this way later.
Interface classes do not need to be referenced
Type
TNOREFCOUNT = Class (TOBJECT, IUNKNOWN)
protected
Function QueryInterface (Const IID: Tguid; Out Obj): hResult; stdcall;
Function _addref: integer; stdcall;
Function _Release: integer; stdcall;
END;
Function Tnorefcount.QueryInterface (Const IID: Tguid; Out Obj): hResult;
Begin
IF GetInterface (IID, OBJ) THEN
Result: = 0
Else
Result: = windows.e_nointerface;
END;
Function TNOREFCOUNT._ADDREF: Integer;
Begin
Result: = -1
END;
Here, I have to spend so many spaces to explain that the interface should be an interface is everything in the component. It will not be a component, but the interface introduced here is less pitiful, I will in the future Detailed description in the article. Do you now have a preliminary understanding of the interface? If you feel a lot of questions and urgently want to know the answer, you can contact me, or look at my previous article on the interface, and introduce the Ding point in the Super Sweeping.
After that, we will make some components, please pay attention to the next article.