In C ++:
In C ++ProgramIn the design, polymorphism means that a function with different functions can use the same function name, so that a function name can be used to call different functions. Or refers to the issue Same message It is generated when it is received by different types of objects. Different execution results and Behaviors .
Message: Call of member functions
Different actions: different function implementations call different functions. Static polymorphism: Function overload, Operator overload, Class Template
Dynamic polymorphism: virtual functions
The problem to be investigated for dynamic polymorphism is: When a base class is inherited into a different derived class, each derived class can Use the same member name as the base class member. Member names that call class objects, The object Member?
Binding: binds a function name with a Class Object to establish an association. Generally, association means to associate an identifier with a storage address.
In the running stage, virtual functions and class objects are "Bound". This process is called dynamic binding ).
Class Father { Public : Father () {printf ( " I am Father \ n " );} Void Read () {printf ( " Father is reading \ n " );}}; Class Son: Public Father { Public : Son () {printf ( " I am son \ n " );} Void Read () {printf ( " Son is reading \ n " );}}; Void Main (){Father * Father = New Son (); // Father calls a function that implements the father class. //// Father * father does not define the object, and all constructors are not called. Fahter. Read (); // Print out father is reading // Will the subclass or parent class be executed? Subclass. During runtime, the corresponding method is obtained based on the actual type referenced by the object. So there is polymorphism. Object introduction of a base class // Used to be referenced by different subclass objects. Different actions are performed when this method is executed. // Father father1 = (father) father; // Error message. Why ????? Father-> Read (); Father father1; father1.read (); // Print I am Father. Father is reading. } Result: I am father I am son father Is Reading
# Include " Stdio. h " Class Father { Public : // Father () // { // Printf ("I am Father \ n "); // } Virtual Void Read () {printf ( " Father is reading \ n " );}}; Class Son: Public Father { Public : // Son (){ // Printf ("I am son \ n "); // } Void Read () {printf ( " Son is reading \ n " );}}; Void Main () {FATHER * Father = New Son (); // Father calls a function that implements the father class. Father-> Read (); // Print I am Father. I am Son. Son is reading. Father father1; father1.read (); // Print I am Father. Father is reading // The following is the implementation of Polymorphism Father * m; // No defined object, no constructor called Father; // Call the parent class Constructor Son B; // Call the parent class constructor before calling the subclass constructor. M = &; // Assign the address of the subclass object to the address of the parent class object. When calling a function, it calls the subclass function. The function in the parent class must be a virtual function. M-> Read (); m = & B; m -> Read ();}
Result:
SonIsReading
FatherIsReading
FatherIsReading
SonIsReading
In Java:
Polymorphism in Java: a dynamic method call is implemented by assigning a subclass object reference value to a super Class Object reference variable.
Public class father {
Public father (){
System. Out. println ("I am Father ");
}
Public void read (){
System. Out. println ("Father is reading ");
}
Public static void main (string [] ARGs ){
String Hello = "hello ";
Father = new son (); // father calls a function that implements the father class.
Father. Read ();
Son son;// No defined object, so no constructor is called
}
}
Class son extends father {
Public son (){
System. Out. println ("I am Son ");
}
Public void read (){
Super. Read ();//To call the function whose parent class is overwritten in the subclass, use the super. format.
System. Out. println ("Son is reading ");
}
}
Result:
I am Father
I am Son
Father is reading
Son is reading
Class{
Public String show (d OBJ )... {
return ("A and D ");
}
Public String show (a obj )... {
return ("A and ");
}
Class B extends {
Public String show (B OBJ )...{
Return ("B and B ");
}
Public String show (a obj )...{
Return ("B and ");
}
}
Class C extends B ...{}
Class D extends B ...{}
A a1 = new ();
A a2 = new B ();
B = new B ();
C = new C ();
d = new D ();
system. out. println (a1.show (B); ①
system. out. println (a1.show (c); ②
system. out. println (a1.show (d); ③
system. out. println (a2.show (B); ④
system. out. println (a2.show (c); ⑤
system. out. println (a2.show (d); ⑥
system. out. println (B. show (B); 7
system. out. println (B. show (c); tables
system. out. println (B. show (d); tables
Answer
① A and
② A and
③ A and D
④ B and
⑤ B and
⑥ A and D
7. B and B
Between B and B
Between A and D
Analysis
① ③ It is easy to understand and generally does not cause errors. ④ It's a bit confused. Why isn't "B and B" output ?!! Let's review the polymorphism.
Runtime polymorphism is an object-oriented programmingCodeOne of the most powerful reuse mechanisms, dynamic concepts can also be said to be "one interface, multiple methods ". Dynamic Method scheduling is the basis for JAVA Implementation of Runtime polymorphism. It is a mechanism for calling overloaded methods at runtime rather than during compilation.
Overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overloading is a manifestation of the polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called overloading ). The overloaded method can change the type of the returned value. Overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overloading is a manifestation of the polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called overloading ). The overloaded method can change the type of the returned value.
When a superclass object references a variable to reference a subclass object, the type of the referenced object, instead of the type of the referenced variable, determines who calls the member method, however, the called method must have been defined in the superclass, that is, the method covered by the quilt class. (However, if the superclass is forced to be converted into a subclass, you can call the method newly added to the subclass but the superclass does not have .)
Now, let's get down to it first! In fact, this involves the priority of method calls. The priority is from high to low: This. show (O), super. show (O), this. show (Super) O), super. show (Super) O ). Let's see how it works.
For example, ④, a2.show (B), A2 is a reference variable, type is A, this is A2, B is an instance of B, so it finds the show (B OBJ) method in Class A and does not find it, so it searches for a's super (superclass), and a does not have a superclass, so it goes to the third priority this. show (Super) O), this is still A2, Here O is B, (Super) O is (Super) B is, therefore, it finds the show (a obj) method in Class A. Class A has this method, but because A2 references an object of Class B, B overrides the show (a obj) method of A, so it is eventually locked to show (a obj) of Class B, and the output is "B and ".
For example, accept, B. show (C), B is a reference variable, type is B, this is B, C is an instance of C, so it goes to class B to find the show (C OBJ) method, not found. I switched to super class A of B and found it in class A, so I also switched to the third priority this. show (Super) O), This is B, O is C, (Super) O is (Super) C is B, so it finds show (B obj) in B) method. B References an object of Class B, so it is directly locked to show (B obj) of Class B, and the output is "B and B ".
According to the above method, other results can be obtained correctly.
The problem persists. Now let's take a look at how the above analysis process shows the meaning of the blue font sentence. It said: When a super-Class Object references a variable to reference a subclass object, the type of the referenced object rather than the type of the referenced variable determines who calls the member method, however, the called method must have been defined in the superclass, that is, the method covered by the quilt class. Take a2.show (B) for example.
A2 is a reference variable of type A, which references an object of B. Therefore, B determines which method to call. Therefore, you should call show (B obj) of B to output "B and B. But why is the result of the preceding analysis inconsistent ?! The problem is that we should not ignore the second half of the blue font, which specifically states that the called method must be defined in the superclass, that is, the method of quilt coverage. Is show (B obj) in B defined in superclass? No! That's not to mention being covered. In fact, this sentence hides a piece of information: it is still determined based on the priority of the method call. It finds show (a obj) in Class A. If subclass B does not overwrite the show (A OBJ) method, it calls show (A OBJ) of Class) (Because B inherits a, although it does not cover this method, it inherits this method from super class A. In a sense, it is determined by B to call the method, only the method is implemented in a); now subclass B overwrites show (a obj), so it is eventually locked to show (a obj) of B ). This is the meaning of the sentence.