Yesterday to write this usage summary, the sudden emergence of a problem, after consulting others, have their own little understanding. or write it down, for everyone better understanding to provide a little thought.
1 someone wrote a very good initialization property of the constructor, and you just want to add some other own initialization of the new property, so that in one constructor call another constructor, you can avoid duplication of code volume, reduce the workload;
2 when invoking another constructor in one constructor, you should use the same memory space, initialize the variable in the default constructor, and overwrite the value of the initialized variable when invoking the other.
3 The entire call process and recursive call function is a bit similar, constant inflatable ball, until the whole balloon swelled up, continuous deep progressive, encounter stop Mark, step by step out.
Wrote a piece of code to explain my above description:
Copy Code code as follows:
Class Javantiger {
int age; Age
int hight; Body Height
Public Javantiger () {
Print ();
this.age=2; This initializes the value of age, but when recursive returns, the value is overwritten
}
Public Javantiger (int age) {
This (); Call your own first constructor, the following two statements are not executed
This.age = age;
Print ();
}
Public Javantiger (int age, int hight) {
This is (age); Call your own second constructor, the following two statements are not executed
This.hight = hight;
Print ();
}
public void print () {//Print function
System.out.println ("I am a" + Age + "old" + hight + "foot high tiger!");
}
public static void Main (string[] args) {
New Javantiger (3,3);
}
}
Output
I ' am a 0 years old, 0 feet high tiger!
I ' am a 3 years old, 0 feet high tiger!
I ' am a 3 years old, 3 feet high tiger!
Personal understanding is this, there may be a problem, such as the constructor recursive call and the procedure function recursive invocation of the mechanism seems to be the same? The constructor is created with the object, that is, the memory space is allocated at the same time, will the recursive call such as the Gate disturb the order of allocating memory? Daniel wants to see the final decision, give the best explanation.
What we're going to summarize today is the use of the Super keyword., super in the constructor is usually the current class inherits other classes, super is to invoke the constructor of the parent class, patch code First
Copy Code code as follows:
Class Tiger {
int age; Age
int hight; Body Height
Public Tiger () {
Print ();
}
public void print () {
System.out.println ("I am a" + Age + "old" + hight + "foot high tiger!");
}
}
public class Javantiger extends Tiger {
Public Javantiger () {
Super (); Calling a constructor with no arguments to the parent class
}
public static void Main (string[] args) {
New Javantiger ();
}
}
In fact, the super () in the constructor of class Javantiger can not write, Java will call the parent class parameterless constructor by default, but if the parent class does not define a constructor without parameters, there is no syntax error, the program automatically exits without any print statements, This time you need to manually invoke the constructor of the other parent class and paste the code:
Copy Code code as follows:
class Tiger {
int age;//Ages
&nb sp; int hight; Body Height
public Tiger (int age) {
this.age = age;
print ();
}
public void print () {
System.out.println ("I am a" + Age + "old" + hight + "foot high tiger!");
}
}
public class Javantiger extends Tiger {
public Javantiger () {
Super (1); //calling constructor with parameters for parent class
}
public static void Main (string[] args) {
new Javantiger ();
}
}
Super (1) in this code must be written in, or the compiler will report an error. So I'm simply summarizing, "this () is calling your own other constructors, super () is the constructor of the parent class that calls itself, and if you only want to invoke a parent class constructor that has no parameters by default, you don't have to write it in the constructor of a subclass, but you'll always ignore this when you're programming.
That door. Can these two keywords also appear in a constructor of a subclass? The answer must be no. First of all, say your own understanding:
1 when creating a new base class, regardless of the recursive call to its own constructors, eventually you will call the constructor of the parent class (not explicitly called, the system will call the default parameterless parent class constructor);
2 in JAVA, the use of this and super must be placed in the first row of the constructor, with only one first line;