"A particularly effective rule when designing the builder is to put the object in a ready state in as simple a way as possible, and avoid calling any methods if possible." The only methods that are safe to invoke in the builder are those that have a final attribute in the underlying class (also for private methods, which automatically have a final attribute). These methods cannot be overwritten, so there is no such potential problem. ”
Excerpt from: (United States) Dr. Eckel Chen Haopeng translation. "Java Programming ideas".
So what is the underlying problem? Take a look at the code:
Abstract class Glypy { static int radius = 2; abstract void Draw (); Glypy () { System.out.println ("Glyph () before Draw ()"); Draw (); System.out.println ("Glypy () after draw ()");} } Class Roundglyph extends Glypy { int radius = 1; Roundglyph (int r) { radius = R; System.out.println ("roundglyph.roundglyph,radius=" + radius); } void Draw () { System.out.println ("Roundglyph.draw (), radius=" + radius);} } public class Polyconstructors {public static void Main (string[] args) { new roundglyph (5);} }
Output Result:
Glyph () before Draw () Roundglyph.draw (), Radius=0glypy () after draw () roundglyph.roundglyph,radius= 5
This begs the question of an initialization order, and for an inherited situation, it is clear that the parent class is initialized first, and then the child class. Coupled with static properties, static blocks, and non-static properties, the order of construction methods.
So if there are both inheritance and static properties static and so on.
Static attributes (post-father subclass)-static blocks (after the Father Class)--Non-static attributes (after the late father subclass)--construction method (after the late father subclass)
The Java initialization sequence and the potential problems of the constructor methods that might be thrown by this.