When I wrote this usage summary yesterday, I suddenly encountered a problem. After asking someone else, I had my own understanding. Write it down to provide a better understanding.
1) Someone has written a good initialization attribute constructor, and you just want to add another new attribute initialization in it, in this way, calling another constructor in one constructor can avoid repeated code and reduce the workload;
2) When calling another constructor in a constructor, the same memory space should be used, and the variables should be initialized first in the default constructor, override the value of the initialized variable when calling another variable;
3) The whole call process is similar to the recursive call function. It continues to be filled with balloons until the whole balloon expands and continues to go deep. When it comes to the stop mark, it jumps out layer by layer.
I wrote a code segment to explain the above description:
Copy codeThe Code is as follows: class JavanTiger {
Int age; // age
Int hight; // body height
Public JavanTiger (){
Print ();
This. age = 2; // The value of age is initialized here, but this value will be overwritten when it is returned recursively.
}
Public JavanTiger (int age ){
This (); // call your first constructor. The following two statements are not executed.
This. age = age;
Print ();
}
Public JavanTiger (int age, int hight ){
This (age); // call your second constructor. The following two statements are not executed.
This. hight = hight;
Print ();
}
Public void print () {// print the Function
System. out. println ("I 'am a" + age + "years old" + hight + "titiger! ");
}
Public static void main (String [] args ){
New JavanTiger (3, 3 );
}
}
// Output
// I'm a 0-year-old, 0-foot-high tiger!
// I'm a 3 years old, 0 feet high tiger!
// I'm a 3 years old, 3 feet tall tiger!
My personal understanding is like this, and there may be problems in it. For example, does the mechanism of constructor recursive call and program function recursive call seem the same? The constructor is generated together with the object, that is, the memory space is allocated at the same time. Will recursive calls like this disrupt the order of memory allocation? Hope you can see the best explanation.
Today we want to summarize the use of the super keyword. When super appears in the constructor, this class generally inherits other classes, super appears to call the constructor of the parent class.Copy codeThe Code is as follows: class Tiger {
Int age; // age
Int hight; // body height
Public Tiger (){
Print ();
}
Public void print (){
System. out. println ("I 'am a" + age + "years old" + hight + "titiger! ");
}
}
Public class JavanTiger extends Tiger {
Public JavanTiger (){
Super (); // call a constructor without parameters in the parent class
}
Public static void main (String [] args ){
New JavanTiger ();
}
}
In fact, super () in the constructor of the class JavanTiger can be left empty. JAVA will call the non-parameter constructor of the parent class by default, but if the parent class does not define a non-parameter constructor, if there is no syntax error, the program will exit automatically without any print statement. In this case, you need to manually call the constructors of other parent classes and paste the Code in the following sections:Copy codeThe Code is as follows: class Tiger {
Int age; // age
Int hight; // body height
Public Tiger (int age ){
This. age = age;
Print ();
}
Public void print (){
System. out. println ("I 'am a" + age + "years old" + hight + "titiger! ");
}
}
Public class JavanTiger extends Tiger {
Public JavanTiger (){
Super (1); // call the constructor with parameters in the parent class
}
Public static void main (String [] args ){
New JavanTiger ();
}
}
Super (1) In this code must be written in; otherwise, the compiler reports an error. So I simply concluded that "this () is used to call other constructors of the current class, and super () is used to call the constructor of the parent class inherited by the current class", if you only want to call the default non-parameter parent class constructor, you do not need to write it in the subclass constructor. However, this is always ignored during actual programming.
Can these two keywords be included in a constructor of the subclass at the same time? The answer must be no. Let's talk about your understanding:
1) when creating a base class, no matter how many times your own constructor is called recursively, you will eventually call the constructor of the parent class. (if it is not explicitly called, the system will call the parent class constructor without parameters by default );
2) JAVA requires that this and super must be placed in the first line of the constructor, with only one first line;