1) Someone writes a nice constructor that initializes attributes, but you just want to add other initializers to it, so that you can call another constructor in one constructor to avoid repetitive code and reduce work;
2) Constructor calls from one constructor should use the same block of memory, initialize variables in the default constructor and overwrite the values of the initialized variables in the other constructor.
3) The whole call process is somewhat similar to recursive call function, continuously inflating the ball until the whole balloon expands, continuously advancing deeper, meeting the stop mark, jumping out layer by layer.
I wrote a code to explain my statement above:
1 class JavanTiger { 2 int age; // age 3 int hight; 5 public JavanTiger() {6 print(); 7 this.age=2; 8} 9 10 public JavanTiger(int age) {11 this(); // call the first constructor of its own, the following two statements do not execute 12 this.age = age; 13 print(); 14 } 15 16 public JavanTiger(int age, int hight) { 17 this(age); This.hight = hight; this.hight = this.hight; this.hight = this.hight; this.hight = this.hight; 19 print(); 20} 21 22 public void print() {system.out.println ("I'm a "+ age +" + hight +" ); 24} 25 public static void main(String[] args) {26 new JavanTiger(3,3); 27} 28} 29 //output 30 //I'm a 0 years 0 feet high tiger! 31 //I'm a 3 years old 0 feet tall Tiger! 32 //I'm a 3 years old 3 feet tall Tiger!Copy the code
Do recursive constructor calls and recursive program calls seem to have the same mechanism? The constructor is created with the object, so it allocates memory at the same time. Does this recursive call disturb the order in which the memory is allocated?
Today’s summary is the use of the super keyword. Super appears in a constructor when the current class inherits from another class. Super appears in order to call the constructor of the parent class and attach the code first
1 class Tiger { 2 int age; // age 3 int hight; 5 public Tiger() {6 print(); 7} 8 9 public void print() {8 system.out.println ("I'm a "+ age +" + hight +" ); 11 } 12 } 13 public class JavanTiger extends Tiger { 14 public JavanTiger() { 15 super(); 17 public static void main(String[] args) {18 new JavanTiger(); 19}} 20Copy the code
Actually in the super class constructor of JavanTiger () can not write, JAVA will default invoked the no-arg constructor of the superclass, but if the parent class does not define a no-arg constructor, no grammatical errors, the program will automatically quit, don’t have any print statements, you need to manually call the superclass constructor, other Post code:
1 class Tiger { 2 int age; // age 3 int hight; 5 public Tiger(int age) {6 this.age = age; 7 print(); 8} 9 public void print() {10 system.out.println ("I'm a "+ age +" + hight +" ); 11 } 12 } 13 public class JavanTiger extends Tiger { 14 public JavanTiger() { 15 super(1); 17 public static void main(String[] args) {18 new JavanTiger(); 19}} 20Copy the code
Super (1) must be included in this code, otherwise the compiler will report an error. So I simply summarize, “this () is to call its own constructor, super() is to call its inherited parent constructor.” If you only want to call the default parent constructor with no arguments, you do not need to write it in the constructor of the child class, but in actual programming, always ignore this point.
Can both keywords appear in a constructor of a subclass? The answer is definitely no. Let me explain my own understanding first:
1) When creating a new base class, no matter how many times you recursively call its own constructor, you will eventually call the parent constructor (if not explicitly, the default parent constructor will be called).
2)JAVA specifies that this and super must be placed on the first line of the constructor, with only one first line;