Binding java method calls -- Notes on java programming thoughts

Source: Internet
Author: User

Bind a method to call the same method subject. There are two types of binding: Pre-binding and post-binding.

Bind -------------

| ----- Pre-bind ------- compile-time bind {static, final method (private method belongs to final method); data member ;}

|

| ------ Post-binding ------- binding at runtime {common member methods ;}

Summary: For methods, except static and final methods, other methods are post-bound.

During compilation, only declared types are checked for references, regardless of actual types.


Function of declaring a method as final -------

{To prevent this method from being overwritten, the compiler is notified that no dynamic binding is required for this method ;}

Problems caused by polymorphism:

Example in java programming ideology (version 4:

class Glyph{void draw(){print("Glyph.draw();")}Glyph(){print("Glyph() before draw()");draw();print("Glyph() after draw()");}}class RoundGlyph extends Glyph{private int radius = 1;RoundGlyph(int r){radius = r;print("RoundGlyph.RoundGlyph(), radius = " + radius);}void draw(){print("RoundGlyph.draw(), radius =  "+ radius);}}public class PolyConstructors{public static void main(String[] args){new RoundGlyph(5);}}/*Output:Glyph() before draw()RoundGlyph.draw(), radius = 0Glyph() after draw()RoundGlyph.RoundGlyph(), radius = 5*/
When a new RoundGlyph object is created, the execution sequence of the constructor is parent class -----> subclass. Therefore, the constructor of the Glyph is first executed,

Then execute the RoundGlyph constructor. However, the Glyph constructor calls the draw member function. Therefore, execute

Member function? No, because of polymorphism, it is bound only during runtime for General member functions, because new is a RoundGlyph object, so

It is bound to the draw function of RoundGlyph. At the same time, the radius of the RoundGlyph member has not been initialized, So 0 is displayed.

The next step is the RoundGlyph constructor, because in the constructor, the radius of the RoundGlyph member is initialized first,

Then run the draw function of RoundGlyph. Therefore, radius is 5.

Conclusion: Try to avoid calling other methods in the constructor to prevent unexpected problems caused by polymorphism.

Now, let's see if this problem will occur in C ++ and change the java code to C ++:

# Include
 
  
Using namespace std; class Glyph {public: Glyph () {cout <"Glyph () before draw () \ n"; draw (); cout <"Glyph () after draw () \ n ";} void draw () {cout <" Glyph. draw (); \ n ";}}; class RoundGlyph: public Glyph {public: RoundGlyph (int r) {radius = r; cout <" RoundGlyph. roundGlyph (), radius = "<radius <'\ n';} void draw () {cout <" RoundGlyph. draw (), radius = "<
  
   
The running result is:
   


The same problem does not occur. It can be seen that C ++ is better at the problem of polymorphism. Learning java has a role, that is, let you

Better understanding of C ++.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.