First, the object-oriented core mechanism-dynamic binding, also known as polymorphic
1.1. Use the following example to understand dynamic binding, which is polymorphic
1 package javastudy.summary; 2 3 class Animal {4/** 5 * Declares a private member variable name. 6 */7 private String name; 8 9/** 10 * Custom Construction method in Animal class one * @param name */Animal (String name) {This.nam e = name; 15} 16 17/** 18 * Customize a method inside the animal class enjoy */public void enjoy () {System.out.pri Ntln ("Animal Cries ..."); 22} 23} 24 25/** 26 * Subclass Cat inherits from the parent class animal, and the cat class has all the properties and methods of the animal class. * @author GACL * * * * * * * Cat extends Animal {31/** 32 * Define your own private member variable inside the subclass cat */P Rivate String Eyescolor; 35 36/** 37 * Define the method of construction of cat class within the subclass Cat. * @param N-Series * @param c + */-Cat (string n, String c) {42/** 43 * In the implementation of the construction method, first use super to call the construction method of the parent class animal animal (String name). 44 * The parent class object inside the object is created first. * * * SUPER (n); Eyescolor = C; 48} 49 50/** 51 * Subclass Cat is not satisfied with the enjoy method inherited from the parent class animal, where it rewrites the ENJoy method. * */Enjoy public void () {System.out.println ("My cat cried with delight ..."); 55} 56} 57 58/** 59 * Subclass Dog inherits from the parent class animal, and the dog class has all the properties and methods of the animal class. * @author GACL * * * */extends Animal {64/** 65 * Define your own private member variable in subclass dog * */P Rivate String Furcolor; 68 69/** 70 * Define the method of constructing the dog class in subclass dog. * @param n * * @param c * * c) {75/** 76 * In the implementation of the construction method, first use super to call the construction method of the parent class animal animal (String name). 77 * The parent class object inside the object is created first. */+ super (n); Furcolor = C; 81} 82 83/** 84 * Subclass Dog is not satisfied with the enjoy method inherited from the parent class animal, where the enjoy method is rewritten. * */Enjoy () {System.out.println ("My dog cried with delight ..."). () 88} 89} 90 91/** 92 * Subclass Bird inherit from the parent class animal, Bird class has all the properties and methods of the animal class. * @author GACL 94 * * * * * * * * * * * Bird EX Tends Animal {97/** 98 * Define the construction method of Bird class in subclass Bird */100 Bird () {101 /**102 * In the implementation of the construction method first use super Call the parent class animal construction method Animal (String name). 103 * The parent class object inside the object is created first. 104 */105 Super ("Bird"); 106}107 108/**109 * Subclass Bird not satisfied with the animal method inherited from the parent class enjoy, overriding the Enjoy method here. */111 public void Enjoy () {System.out.println ("My bird cried with delight ..."); 113}114}115 116/**117 * definition A class Lady (Lady) 118 * @author gacl119 *120 */121 class Lady {122/**123 * defines a private member variable for the Lady class name and pet124 */125 Private String name;126 Private Animal pet;127 128/**129 * Define your own method of construction in The Lady Class Lady (), 130 * This constructor has two parameters, respectively For string type name and animal type pet,131 * The second parameter here is set to animal type can give our program maximum flexibility, 132 * Because as a pet, you can keep a cat, a dog, a bird, as long as you like to keep, 133 * Therefore it is most flexible to set it as a reference to the parent class object. 134 * Because the parameter of this animal type is the reference type of the parent class object, when we pass the parameter, 135 * can pass the subclass object of the parent class, that is, the dog, cat, and bird, etc. can be passed. 136 * @param name137 * @param pet138 */139 Lady (String name, Animal Pet) {this.name = name;141 This.pet = pet;142}143 144/**145 * Customize a method inside the Lady class Mypetenjo Y () 146 * Method The body is to let the pet of the lady to call their own enjoy () method to make their own calls. 147 */148 public void Mypetenjoy () {149 Pet.enjoy (),}151}152 153 public class Testpolymoph {154 public static void Main (String args[]) {155/**156 * in heap memory new A blue cat object comes out, this blue cat object contains a parent class object animal. 157 */158 Cat C = new Cat ("CatName", "Blue"); 159/**160 * In heap memory new A black dog object comes out, this black dog object contains A parent class object animal. 161 */162 Dog d = new Dog ("Dogname", "Black"); 163/**164 * In heap memory new A bird object comes out, this bird object contains There is a parent class object animal. 165 */166 Bird B = new Bird (); 167 168/**169 * In heap memory new out of 3 little girls, the name is L1,L2,L3. * L1 a pet is C (Cat), L2 a pet is D (dog), L3 a pet is B (Bird). 171 * Note: When invoking the constructor method of the Lady class, the passed-in c,d,b is passed as animal, 172 * So using c,d,b the three reference objects can only access the enjoy () method inside the parent class animal. 173 */174 Lady L1 = new Lady ("L1", c); 175 lady L2 = new lady ("L2", d); 176 lady L3 = new Lady ("L3", b); 177/ **178 * These three little girls all call the Mypetenjoy () method to make their pets happy to call up. 179 */180 L1.mypetenjoy (); 181 L2.mypetenjoy (); 182 l3.mypetenjoy (); 183}184}
Operation Result:
1.2. Draw memory diagram to understand dynamic binding (polymorphic)
First, start with the first sentence of the Main method:
Cat C = new Cat ("CatName", "Blue");
Program execution here, there is a variable in the stack space C,c contains a series of values, which can be found in the heap in the memory of the new Cat object. So C is a reference to the Cat object, and all of this cat object can be seen through C. C points to the new Cat object. In the new Cat object, the method of constructing cat objects called Cat (String n,string c) is defined as follows:
Cat (String n,string c) {
Super (n);
Eyescolor=c;
}
Therefore, when constructing a subclass object, first use the reference to the parent class object Super calls the parent class's construction method, animal (String name), as defined below:
Animal (String name) {
This.name=name;
}
The string "CatName" passed to the parent object's Name property. When the cat (String n,string C) Constructs a method call, it really is in the heap memory new out of a cat, the cat contains a parent object animal, the animal object has its own property name, The value of the Name property is the string CatName passed when the parent class constructor method is called. Besides, this cat also has its own private member variable the property value of the Eyescolor,eyescolor property is the string that is passed when the subclass constructor method is called Blue. So after executing this sentence, the in-memory layout is the stack memory with a reference c,c pointing to the heap in the memory of a new cat, and this Cat object contains a parent object Animal,animal object has its own property name, the property value is CatName, In addition to having the name attribute inherited from the animal class, cat has a private property eyescolor, with a property value of blue. This is how the entire memory layout after the first sentence is executed, as shown in the following scenario:
Then look at this sentence: Lady L1 = new Lady ("L1", c);
The program executes here, first in the stack memory inside a reference variable L1,L1 contains a value, through this value can be found in the heap memory of new Lady object. L1 is the reference to this Lady object, and L1 points to the Lady object. When you create a Lady object, you invoke the constructor method of the Lady class: Lady (String name,animal Pet), which is defined as follows:
Lady (String name,animal Pet) {
This.name=name;
This.pet=pet;
}
This constructor method has two parameters, the string type name and the animal type of the Pet,pet parameter is a reference type of the parent class object, where L1 and C are passed as arguments to the construction method, and then the this.name=name is executed inside the constructor method. Pass the L1 passed to the Lady object's Name property, so the Lady object's Name property value is L1, here also the previous new cat's Reference C passed to the constructor method inside the parameter pet, and then in the construction method inside the implementation of This.pet=pet, The pet parameter passes the contents of C to the pet property of the Lady object, so the attribute value of the pet attribute is that the address of the Cat object can be found, so the pet attribute of the Lady object is also the reference object of the Cat object, and the value that can be found by pet is the cat object. So pet also points to cat, but not all point to the animal object that Cat,pet points to only the inside of the cat object, because when the constructor method is called, C is passed as a reference to a animal object, and C is transferred to pet as a animal object. So the resulting pet is also a reference to the animal object, so the pet reference can only be directed to the animal object located inside the Cat object. In the eyes of my pet Reference object, your cat object is an ordinary animal, access you can only access the Name property inside you, and your Eyescolor property I am not access to, I can access to your Name property, Access is the Name property of the parent object inside of you, because my pet reference itself is a reference to a parent class object, so I can access all the properties of the parent class object, and your subclass object cat own The newly added member my pet reference is not accessible. But now my pet reference does not go to the member variable of your parent class object name, but to access your member method enjoy. The first is to use the Lady object's reference L1 to invoke the Mypetenjoy () method of the Lady object, as defined by the Mypetenjoy () method:
public void Mypetenjoy () {
Pet.enjoy ();
}
Then, in the Mypetenjoy () method body, a pet reference object is used to invoke the Enjoy method inside the parent class object.
method is placed in the code SEG, inside the method is a sentence code. So whenusing Petwhen referencing a method that accesses a parent class object, the first is to find the parent object, and then see where it is stored, find the method , and then execute it.。 Here the head is more interesting, there are many enjoy methods in the code SEG, there is the enjoy () method of the parent class, and there are subclasses that rewrite the enjoy () method to continue from the parent class, then which one is called at the time of the call? According to who is to determine it? Note: This is based on your actual object to determine, you actually in the new out of who, call whose enjoy method, when you look for this method, through the pet reference can find this method, but the call code area in which the enjoy method is not determined by reference type, If it is determined by reference to the type of pet, then the call is definitely animal's enjoy () method, but now it is determined by the actual type, and our program is running to create a cat in the heap memory, Then judge which enjoy () method I should call based on the type of new you actually have. If it is based on the actual type, then the enjoy () method of the cat should be called. If it is based on the reference type, then the call should be animal's enjoy () method. Now the dynamic binding of this mechanism refers to the actual type of what is new, and call whose enjoy method. So while you are calling according to the Enjoy method inside my parent, it is actually the enjoy () method that you are new to who is calling. That is, the actual call is the subclass of the rewrite of the Enjoy method. Of course, when you actually look for this enjoy method in a deeper mechanism, there is a pointer to the Enjoy method inside the parent object that points to the enjoy method of the animal of the parent class in the code area, but when you new the object, the pointer changes, What is your new object, this pointer points to the enjoy method after the object is rewritten, so this is called dynamic binding. Only when you are moving, that is, when the program is running, new is out of this object and you can determine which method to invoke. My actual address is bound to the corresponding method of the address above, so called dynamic binding. When you adjust this method, as long as you rewrite the method, the actual one to adjust, to see you in the actual new object, which is called polymorphic, also known as dynamic binding.The great benefit of dynamic binding is that the scalability of the program is achieved with the best, when we originally did this extensibility, first of all is to judge in the method what kind of animal this animal is, through the if (object instanceof Class) such conditions to determine which of the new object belongs to which class, if it is a cat, Just call the cat's Enjoy method, and if it is a dog, call the dog's Enjoy method. If I add a bird class now, then you have to write in the method to determine which class the bird belongs to and then call the enjoy method of the bird. For each additional object, you need to add a piece of code inside the method to determine which class the object belongs to and then execute the object's corresponding method. That is, every new object that you add will change the processing code inside the method, and now you don't have to change the processing code inside the method because of the dynamic binding. Which object do you want to add, you actually put this object new out of the way, and no longer use to modify the object's processing inside the code. That is, when you actually want to add something else, very simple, you directly add to become, do not have to change the original structure, you have to build a kitchen next to your house, very simple, directly in the side of a lid on the line, the main pillars of the building you do not have to move, which can allow scalability to reach the extreme, This lays the groundwork for future scalability,also only dynamic binding (polymorphic) This mechanism can help us do this--to maximize the scalability of the program. So dynamic binding is the core of object-oriented, and if there is no dynamic binding, then object-oriented is absolutely impossible to develop as it is now, so dynamic binding is the core of object-oriented core.
summarize dynamic binding (polymorphic): Dynamic binding refers to the actual object type that is referenced during execution, not during compilation, and calls its corresponding method according to its actual type. so the actual way to find the method to call is dynamic to find, new is who is looking for who's method, this is called dynamic binding. Dynamic binding helps our programs achieve the ultimate in scalability.
There are three necessary conditions for the existence of polymorphism:
- To have inheritance (there is an inheritance relationship between two classes, the subclass inherits the parent class)
- To have overrides (override the method inherited from the parent class within the subclass)
- Parent class reference to child class object
Once these three conditions are met, when you invoke the overridden method in the parent class, the actual child object of new is called, and the method of the subclass object is invoked (this method is the overridden method after inheriting from the parent class).
Object-oriented comparison emphasizes the relationship between class and class, object and object, if you can organize this organization to be better, your program wants to expand better, more robust, better maintainability of these can be achieved, the key to see whether your design is good or not.
Thanks for the guidance of the aloof wolf.
(RPM) The most important mechanism of object-oriented-dynamic binding (polymorphic)