Second, the interface-2004.03.29 ~ 2004.03.30
Interface, when this word is first contact, I am difficult to understand this word. Because it sounds more images related to hardware. There is also a word COM. It means that the meaning of the COM appeared in different fields is very different. However, after understanding the word of the interface, I have to say that it is very appropriate, and the image has explained its nature and use. Just great, rich Chinese opinion is too deep.
Why use the interface? Don't you do it? I am not qualified to give you an answer, but I know that the appearance of the interface solves the following problems. We all know, in addition, the interface is a mechanism, which is more distinct in the language of Delphi. Because the interface only declares / defines methods, not implementation. Abstract in Java is similar to an instead of an interface, but my understanding is that the latter will highlight its neat advantage after the inheritance level increases.
In Delphi, the interface is added from Delphi3 and gradually uses it to enrich VCL. When Delphi5, Borland's interface technology has been perfect in RAD. Well, of course, Delphi5 is the best version of RAD. The subsequent version turned to the richness of the web function, the big name Ding Web Service Technology Delphi is implemented with the interface. 5.0, the old ancestors of the interface were iunknown. It looked that this is a nasty noun, which is the one I just mentioned against Microsoft. When delphi6 is launched, the positive value of Linux wolf is four, Borland promotes Kylix in order to cross the platform strategy, this ancestors changed a large number, called Iinterface. I just started KYLIX, I saw the extensive application of the interface.
We all know that objects in delphi can't live automatically, it doesn't have Java and C Sharp garbage collection mechanism. According to Mr. Li Wei, Delphi's Father Anders Hejelsberg originally designed Delphi, let the object variable write into the heap instead of the stack, just want to let Delphi can automatically recycle idle objects in the future. And until today, Danny Thorpe has not let Delphi implement this feature (although Delphi without automatic recycling is a few close perfect). Instead anders Hejelsberg realized their own ideas after Microsoft, because this feature has been implemented in C Sharp. It can be seen that C Sharp is the unsatisfactory of Anders.
Oh, since it is a chat, it is inevitable to run questions. Saying that automatic goal is to say that in Delphi, the interface can be automatically deactivated. The objects of the interface calculate the number of references to automate the management of memory. Delphi's programmers make better understand the reference technique (REFERENCE COUN).
Let's take a look at the interface in Delphi.
Let us define the interfaces and classes:
Type
Imyidol = Interface
['{30DDAB80-81BF-11D8-A27B-930F92738A22}'] // Shift Ctrl G Generate GUID
Procedure name (s: string); / / interface method does not instantiate
Procedure taff;
END;
Type
TANDERS = Class (TinterFaceDObject, Imyidol)
// Inherit a universal class TINTERFACEDOBJECT and implement interface iMyidol.
Procedure name (s: string); // Shift Ctrl C
Procedure taff;
END;
Then implement:
Procedure tanders.name (s: string); // Shift Ctrl C automatically generates Begin
Form1.edit1.text: = s;
END;
Procedure tanders.others;
Begin
END;
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR
Anders: tanders;
Begin
Anders: = tanders.create;
Anders.name ('Anders Hejlsberg!');
Anders.Free;
END;
Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);
Var Liwei: iMyidol;
Begin
Liwei: = tanders.create;
Liwei.name ('Liwei!'); // Don't have to free;
END;
This is an effect picture.
Then let's take a look at how to implement the interface in Java, then let's talk about talking.
Interface myidol {
Void name (String S);
Void Others ();
}
Class superman {// Define a class
}
Class Man Extends Superman IMPLEments MyIdol {
// Extends inherits the parent class; imports implements the interface. Here is to demonstrate it, it can actually do not inherit.
Public void name (string s) {
System.out.println (s);
}
Public void tares ();
}
PUBLIC CLASS DO {
Public static void main (String [] args) {
MAN man = new man ();
Man.Name ("Anders Hejlsberg!");
}
}
Look at the renderings.
We saw. Delphi classes do not write specific to how to implement when declaring, but to write to Implementation. Java doesn't have to be "trouble" and think where to write. This is their respective features. It is also, therefore, a rigorous, a flexible. Another problem is that the class can inherit multiple interfaces. Can the interface not inherit multiple interfaces? The answer in java is sure, and Delphi7 tells you the wait, huh, huh. It can be seen that Java's implementation of the interface is the most perfect. In addition, Java does not have Delphi's GUID because it does not need to support COM, nor does it need to inherit from the ancestral interface as Delphi.
Going back, observing the code we will find that the Others method is redundant, but it is empty code in Delphi and Java. This is excessive? Not much. In fact, the interface is a bit selfish, it allows the class that inherits its class must implement it, otherwise this class can only be an adapter or abstract class. Oh, remember this. So I didn't dare to create TsuPerman in Delphi, but using a universal class TinterFaceDObject. Otherwise, I have to implement these: queryinterface, _addref, _Release because it is defined by the old ancestors of the interface. What is the way to connect the ancestral eight-generation definitions to achieve this new category just born? Yes. Fortunately, there is a class such as TinterFaceDObject. Let's take a look at it.
TinterFaceDObject = Class (Tobject, Iinterface)
protected
FREFCOUNT: Integer;
Function Queryinterface (Const IID: Tguid; Out Obj): hResult; stdcall; function _addref: integer; stdcall;
Function _Release: integer; stdcall;
public
Procedure afterconstruction; Override;
Procedure BeforeDestruction; Override;
Class Function Newinstance: TOBJECT; OVERRIDE
Property Refcount: Integer Read Free;
END;
Also, all methods defined by interface are public, and constants in the interface are public static and final, and these are not to be explained.
Finally, let's study how to get the interface and call it.
Var Liwei: iMyidol;
Begin
Liwei: = Tanders.create; // Tanders Developed to interface Liwei.
LiWei.Name ('liwei!'); // Call the Name method.
This is an implicit operation method, let's take a closer way to write:
Var Anders: tanders; liwei: iMyidol;
Begin
Anders: = tanders.create; // Generate an object. Liwei: = iMyidol (Anders);
Liwei.name ('liwei!');
What to explain is that Anders must support iMyIdol, otherwise error.
You can also write this:
Var Anders: tanders; liwei: iMyidol;
Const iLiwei: Tguid = '{30DDAB80-81BF-11D8-A27B-930F92738A22}'
Begin
Anders: = tanders.create; // Generate an object.
If Anders.GetInterface (iLiwei, Liwei) THEN
Liwei.name ('liwei!');
END;
The situation in Java is the same:
MAN man = new man ();
Man.Name ("Anders Hejlsberg!"); // This is a way of class object implementation.
Myidol lw = new man ();
LW.NAME ("liwei!"); // This is an interface implementation.
Talk to here, I believe we have a certain understanding of the interface. However, the topic of the interface is too big, and there are many things that are involved, so it is impossible to talk too much too much. Is it a bit thirst? Go and drink some green tea, solve thirsty than coffee.