First, polymorphism-2004.03.28
At the university, I have given a pair of twin brothers to the English class. The two of them are still clear. My brother, I gave him an English name called David. He is very good, it is also very easy to say; whose brother Davis is a bit, but it is very smart, it is better to receive from acceptance. In terms, David is more like Jiang Wen, Davis is more like Jiang Wu, huh, I like two Chinese pure men! This is to find out after they. When I first saw them, I often have to ask, who is your brother.
Well, talking about this little thing is to talk about the polymorphism in delva.
Let's talk about the problem of overloading first (also called overload, in short, it is overload) In the same class, the phenomenon of the method of multiple names is Overload. Contact the brothers mentioned above, I think this is better understood. They are similar from the appearance, different "parameters", which may vary differently on the type.
In Delphi, all methods are static by default, which is convenient for compiler and linker parsing calls. To achieve overload, you must add keyword overload after the method is clear, or an error will occur. First look at a small example:
TYPE // This is the definition part of Delphi to class TA
TA = Class
public
Procedure Xiongdi (AGE, Height: Integer; overload; // This is two common processes of TA, the name
Procedure Xiongdi (Name: String); OverLoad; // But their parameters are different.
END;
Implementation // The following is a part
Procedure TA.XIONGDI (AGE, HEIGHT: Integer); // This is the implementation of two processes.
Begin
Form1.EDit1.text: = 'Age:' INTOSTR (AGE) 'Height:' INTOSTR (HEIGHT);
END;
Procedure TA.XIONGDI (Name: String);
Begin
Form1.EDit2.text: = 'name:' name;
END;
{DELPHI with a strong graphical control, we use a form's Button control to activate the above process and display the result with the Edit control. } Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR
A: Ta; // This is the definition of Delphi to the object, and A is an instantiation of TA, which is an object of the class of TA.
Begin
A. xiongdi (15,130);
A.XIONGDI ('David');
This is an effect picture.
We see the first Xiongdi reflects age and height information, and the second XionGDI reflects name information. Their number and types of their parameters are different, and the effects to express are different. So, in the face of OverLoad we don't want to be confused by the names on the surface, and you should learn about functions, process and methods. Many said that the function and the process are called the method in delphi. Java has become a method, and the method of non-return value is represented by Void keyword, and the method of returning value is the function in delphi.
Let's talk about OverLoad in Java. Java's method and Delphi are just the opposite, all methods are default into virtual, so you can keep OverLoad at any time in Java without indicating that I have to overload this method. For comparison, I wrote one of the same examples in Java: a.java file:
Class a {// This is the definition of class A in Java.
Public void xiongdi (int Age, int high) {// defines two simultaneous methods
System.out.println ("Age:" Age "HIGH:" HIGH);
}
Public void Xiongdi (String Name) {
System.out.println (Name);
}
}
Do.java file:
PUBLIC CLASS DO {
Public static void main (String [] args) {
A a = new a ();
A. xiongdi (15,130);
System.out.println ("**********");
A.XIONGDI ("David");
}
}
Compile and execute the results.
Ok, we have a sense of sensibility to Delva's OverLoad. Gossip less, first in Delphi, you can cover a small example:
Type
Tfather = Class // We define a parent class
public
Function throwhand: String; Virtual;
END;
Type
Tson = class (tfather) // We define a subclass of inherited parental class
public
Function throwhand: String; Override;
END;
// This is the implementation of two functions:
{Tfather}
Function Tfather.ThrowHand: String;
Begin form1.edit1.text: = 'right'; end;
{Tson}
Function Tson.Throwhand: String;
Begin form1.edit2.text: = 'left'; end;
// This is the call to the function:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR Father; SONUSE: TFATHER;
Begin
FatherUse: = tfather.create;
SONUSE: = TSON.CREATE;
Fatheruse.ThrowHand;
SONUSE.THROWHAND;
FatherUse.Free;
SONUSE.FREE;
END;
This is an effect picture:
We see that the parent class is modified by a keyword called Virtual. What is this? Do you want to affiliate? Of course, the reason is that the method of Delphi is static. We know that in Java, the method of trimming Abstract or final is not covered. It can be seen that the default static method of Delphi cannot be covered. Therefore, Delphi defines two keywords: Virtual, and Dynamic. They make functions and processes into dynamic for post-association. Virtual and Dynamic effects are substantially the same. Only the method of Virtual is larger than the Dynamic method in the VMT, and the execution time is short. This is because the Subclass is in a Virtual method in the Override parent class, and the VMT will provide the subclass of the Virtual method that is not available to other parent classes of Voerride. Dynamics only provides methods of Override. These two ways are advantageous, and they are often used together in Delphi. Well, think more. If a Virtual method of the parent class is not only the subclass of the method Override, but also by the same name in the same class, how do you do it? Take a look at the example below:
Type
Tfather = Class
Public // More Keywords Overload Function Throwhand: String; Overload; Virtual;
Function throwhand (M: Integer): integer; overload; virtual;
END;
Type
Tson = Class (TFather)
Public // More Keyword Reintroduce, I found no need to do it. But it is recommended to use.
Function throwhand: String; Reintroduce; Override;
END;
Let's take a look at the implementation section:
{Tfather}
Function Tfather.ThrowHand: String;
Begin
Result: = 'Right';
END;
Function Tfather.ThrowHand (M: Integer): Integer;
Begin
Result: = m;
END;
{Tson}
Function Tson.Throwhand: String;
Begin
Form1.edit2.text: = 'Left';
END;
This is the call part:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR
FatherUse: tfather;
SONUSE: TSON;
Begin
FatherUse: = tfather.create;
SONUSE: = TSON.CREATE;
Form1.edit1.text: = 'UseHand:' FatherUse.ThrowHand 'Throwfar:' INTOSTR (FatherUse.Throwhand (500)) 'M';
SONUSE.THROWHAND;
FatherUse.Free;
SONUSE.FREE;
END;
Ok, Delphi's coverage is here, 嘻嘻, should have a cup of coffee to enjoy. Here is the coverage of Java, which can not have so many keywords.
This is the code of do.java file:
Class A {// This is a parent class
Void throwhand () {
System.out.println ("Right!");
}
Class Sa EXTENDS A {// This is a subclass
Void throwhand () {
System.out.print ("Left!");
}
}
PUBLIC CLASS DO {
Public static void main (String [] args) {
A a = new a (); // parent class instantiation;
SA S = new sa (); // subclass is instantiated;
System.out.println ("-----------------------");
System.out.print ("Father Throwhand IS:");
A.THROWHAND (); // call
System.out.println ("");
System.out.print ("SON Throwhand IS:");
S.THROWHAND (); // call
}
}
This is an effect picture:
At this point, I will overcome the polymorphic GAB. I hope that you are not annoyed by my text. If you are really annoying, then the series of articles, hey, don't look.