// The following I combined with the instance of the dynamic implementation problem in Java Class Base {public int Ageofbase = 10; public void display () {system.out.println ("Display () in base!");} Public void display1 ) {System.out.println ("Display1 () in base!");
Private void show () {system.out.println ("Show () in base!");}}
Class Test Extendes Base {public int agent = 10; / * This function rewrites the parent class method but he is not simply completing his own function; he also retains the function of the parent class while achieving its own functionality **** ******************************************************************************************************************************************************************************* In this way, only the method of existence is actually inherited from the parent class, just a simple implementation of the functionality of the parent class. * / Public void display () {system.out.println ("Display () in test!"); Super.display ();} // Note I did not rewrite the method display1 (), but this method is due to inheritance // I still exist in subclass TEST // I write a new method for subclavab public void display2 () {// This method does not exist in subclass here //, Be sure to remember: // Access the private member of the parent class is not allowed //super.show (); // Here, the common method of the parent class // In fact, the reference is the method in the subclass // because this The method has inherited Display1 (); // below from the parent class, it is also known as the method of truly reference the parent class // (); // This point may not be very clear // I have changed an example // This is a subclass of Display (); // This is the method of the parent class super.display (); system.out.println ("Display2 () in Test!");} Public static void main (string args []) {test t = new test (); base b = new base (); // This sentence is the fundamental /// one parent class reference points to subclass b = T; // This way to call the category of the parent class // is actually the method implemented in the execution subclass / * Below I combined with the master-oriented programming and research, I can't. Ensure that every point I said is correct, just give you a reference to the concept of object-oriented programming field for polymorphism. I combined with C and do some descriptions in the actual situation in Java. (1) It is best to have a certain understanding of the virtual pointer and the virtual pointer form. If you don't know, you can only remember what I said, but it is not necessarily understood. (2) The parent class references points to the subclass. The method of the subclass is performed when the method is called. This statement has many prerequisites and unexpected. Let me give an example * / b.display ();
// **************** The original article shows an error to start ************* // below The call will be wrong ()
Too //t.display2 ();/******************* The original expression error end **************** ***
/ / Should now be changed to the following description
// The following line should be changed to b.display ()
// This can only be combined with the example of this paper. At the same time, the following line can run.
// For such writing errors, the author means solemn apology
// ******************************************************** ************
// Should be like the following ((TEST) b) .display2 (); int Temp1 = B.AGEOFBase; // The following call will be error // int Temp2 = T.AGEOFTEST; / / should be int temp2 = ((( Test) b) .ageoftest; / * Now explain the reason why the above two will errors, this is the root of the dynamics. I do my best to explain clear. ************** Key 1 ****************** Because the number of objects of the parent class and subclass is in memory Different, of course, different objects itself cannot be the same memory address. This should have no problem. Of course, if the subclass is just a simple inheritance, there is no, it may be the same, but there will be a difference in the compiler in C , and may be different. of. *************** Key two ********************************************************* More deeper is that the reference is to point to the pointer to the pointer, and the memory space size managed by different types of pointers is different, which is tightly contacted above. Note that this is the memory management of the pointer instead of a pointer, I think everyone knows that the pointer is only 4 bytes of length. Here we can simply say the visibility problem of variables, because the variables exist but is not necessarily visible. ************** Key third ********************* The key to dynamic binding To put it bluntly, it is a virtual function virtual pointer virtual pointer table and other important concepts. Simply put, it is very complicated here, but every real object-oriented programmer must take a good understanding of this. There are only two types of static functions and virtual functions in Java, and there is a non-static non-virtual function in C . So Java's dynamic binding is easy. You can understand this inaccurate (because I am very lazy, you can write a long article). When you compile, you will generate a virtual function table, save all the virtual function addresses in your own class. At the same time, if this class has a parent class (regardless of the extends class or imports interface, he will also inherit the virtual function table inherited from the parent class (here I just explain, not very accurate). Please pay attention to this place: *************************************************** *** If the subclass does not rewrite the parent class inheritance, the corresponding virtual function address in the parent class virtual function is turned into a subclass's own virtual function address; if there is no change, it is not necessary to change the virtual function table corresponding to the inheritance. address. ********************************************************