Parsing the usage analysis of super in Java

  • 2020-04-01 02:03:42
  • OfStack

Yesterday when writing this usage summary, suddenly produced a question, after consulting others, had own little understanding. Let me write it down to give you a little bit of perspective.
1) someone wrote a good initializer constructor, but you just want to add other initializers of new properties, so that you can call another constructor in one constructor, which can avoid repetitive code, reduce the workload;
2) when calling another constructor in one constructor, the same memory space should be used. In the default constructor, the variable should be initialized first, and the value of the initialized variable should be overridden when calling another constructor.
3) the whole process of the call is similar to the recursive call function, in which the ball is inflated continuously until the whole balloon is inflated, and the ball continues to progress deeper and deeper until it encounters the stop mark and jumps out layer by layer.
Wrote a piece of code to explain what I said above:

class JavanTiger {
     int age;      //age
     int hight;    //Body height

     public JavanTiger() {
         print();
         this.age=2;   //This initializes the value of age, but when the recursion returns, the value is overridden
     }

     public JavanTiger(int age) {
         this();      //Calls its first constructor and the following two statements are not executed
         this.age = age;
         print();
     }

     public JavanTiger(int age, int hight) {
         this(age);   //Call your second constructor & NBSP; , the following two statements are not executed
         this.hight = hight;
         print();
     }

     public void print() {  //Print function
         System.out.println("I'am a " + age + " At the age of  " + hight + " Feet tall  tiger!");
     }
     public static void main(String[] args) {
         new JavanTiger(3,3);
     }
 }
 //output
 //I'm a 0 years old 0 feet tall tiger!
 //I'm a 3 years old 0 feet tall tiger!
 //I'm a 3-year-old 3ft tall tiger!

So that's the personal understanding, and maybe there are questions, like does the mechanism for recursive calls to constructors and recursive calls to program functions seem to be the same? The constructor is generated with the object, which means that the memory space is allocated at the same time. Does a recursive call like the gate disturb the order in which memory is allocated? Hope to see the cattle clapboard, give a best explanation.
What I want to summarize today is the use of the keyword super. When super appears in the constructor, the current class usually inherits from other classes. The appearance of super is to call the constructor of the parent class

class Tiger {
     int age; //age
     int hight; //Body height

     public Tiger() {
         print();
     }

     public void print() {
         System.out.println("I'am a " + age + " At the age of  " + hight + " Feet tall  tiger!");
     }
 }
 public class JavanTiger extends Tiger {    
     public JavanTiger() {
         super();    //Calls the parent class's parameterless constructor
     }
     public static void main(String[] args) {
         new JavanTiger();
     }
 }

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 exit, without any print statements, you need to manually call the superclass constructor, other stick piece of code:

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 + " At the age of  " + hight + " Feet tall  tiger!");
     }
 }
 public class JavanTiger extends Tiger {    
     public JavanTiger() {
         super(1);    //Calls the constructor with arguments for the superclass
     }
     public static void main(String[] args) {
         new JavanTiger();
     }
 }

Super (1) in this code must be included or the compiler will report an error. So I simply concluded, "this () is to call your other constructor, and super() is to call your inherited superclass constructor." if you just want to call the default superclass constructor with no arguments, you don't have to write it in the subclass constructor.
Can these two keywords be in a constructor of a subclass at the same time? The answer is no. First, let's talk about my own understanding:
1) when creating a new base class, no matter how many times you recursively call its own constructor, eventually you will call the constructor of the parent class, (if not explicitly called, the system will call the default parent constructor without parameters);
2) it is stipulated in JAVA that the use of this and super must be placed on the first line of the constructor, with only one first line;

Related articles: