Java constructor talk of succession

Java constructor talk of succession

Initialize the base class

As mentioned earlier, inheritance is to expand the subclass of the parent class. "Thinking in Java" is mentioned in the following passage:

When creating a derived class of an object, which object contains a sub-object base class. This sub-objects and objects that you use to directly create a base class is the same. Distinction is that in the latter from the outside, while the base subobject is packaged inside the object's derived class.

When we create a subclass of object, call the constructor of the parent class, even the parent of the parent class constructor. We know that the constructor used to create the object, then suddenly wondering: When on creating a subclass object, whether it will create the parent object?
After find information, it concluded that :
do not . When you create a subclass object, member variables and methods of the parent class will be loaded into memory, if it wants to load, then call the parent class constructor and see how the data is initialized, and nothing, not create a parent class Object.
Therefore, it can be seen, the child class object contains child object of the parent class. We know that initialize the object is crucial. So, how this child object of the parent class initialization correct? By the way, I am going to say: call the base class constructor in the constructor to perform initialization .
Note: subclass does not inherit the parent class constructor, the initialization code simply calls the base class constructor is.

The default constructor

Look at a simple piece of test code:

package com.my.pac13;
/*继承中的构造*/
public class Person {
    Person(){
        System.out.println("Person()");
    }
}
class Student extends Person{
    Student(){
        System.out.println("Student()");
    }
}
class PrimaryStudent extends Student{
    PrimaryStudent(){
        //super();
        System.out.println("PrimaryStudent()");
    }
    public static void main(String[] args) {
        //创建了PrimaryStudent对象
        new PrimaryStudent();
    }
}
/*
 Person()
 Student()
 PrimaryStudent()
*/

About the constructor, we mentioned earlier, without any explicit class constructor there are no parameters a default constructor. We added the above example printout default constructor, in order to understand.
We can see that:

  • In creation PrimaryStudent, his direct parent Studentand indirect parent class Personin the constructors are called, and you can see, it is the " top-down " of.
  • Before the parent class constructor subclass can access it has completed the initialization operation.
  • If there is no sub-class constructor explicitly call parent class, then automatically call the default (no reference) of the parent class constructor.

With constructor arguments

Previous code, each class contains a default constructor, when creating a subclass object, from top to bottom, and the sub-class will call the parent class's default constructor with no arguments. So, do not just assume that the parent class constructor with no parameters or you're trying to call the parameterized constructor of the parent class, then you need our super keyword. (Also summarized after the super keyword)
we slightly modified directly on the basis of the original, and tested.

package com.my.pac13;
/*调用基类构造器是子类构造器中要做的第一件事*/
public class Person {
    //没有默认构造器
    Person(String name){
        System.out.println("Person()\t"+name);
    }
}
class Student extends Person{
    //也没有默认构造器,且用super显式调用
    Student(String n){
    //super关键字调用父类的构造器
        super(n);
        System.out.println("一参数Student\t"+n);
    }
    Student(String n,String m){
    //this关键字调用同一类中重载的构造器
        this(n);
        System.out.println("二参数student()\t"+m);
    }
}
class PrimaryStudent extends Student{
    //隐式调用父类构无参数构造器,但是父类没有,所以要用super显式调用
    PrimaryStudent(){
    //没有下面的语句会报错
        super("hello");
        System.out.println("PrimaryStudent()");
    }

}
class ExtendsTest{
    public static void main(String[] args) {
        new Person("the shy");
        System.out.println("***********");
        new Student("rookie");
        System.out.println("***********");
        new Student("the shy","rookie");
        System.out.println("***********");
        new PrimaryStudent();
        System.out.println("***********");
    }

}
/*
Person()    the shy
***********
Person()    rookie
一参数Student  rookie
***********
Person()    the shy
一参数Student  the shy
二参数student()    rookie
***********
Person()    hello
一参数Student  hello
PrimaryStudent()
***********
 */
  • this is the object that is being created for calls in the same class overloaded constructor , you can see my previous article: this of the Java keyword .
  • super when calling the constructor, and the use of this method is similar. (But super and this itself is fundamentally different, super is not an object reference !!!)
  • super and this statement must appear on the first line, that is to say a constructor can only have one of them .

Subclass call the parent class constructor

Whether or not use the super statement to call the parent class constructor initialization code, sub-class constructor will always call the parent class constructor in advance ! This is sure to remember!

  • A sub-class constructor call the parent class constructor B, the first row format explicit use Super super(参数列表), select the corresponding parent class constructor according to the parameter list.
//父类
 Person(String name){
        System.out.println("Person()\t"+name);
    }
//子类
 Student(String n){
    //super关键字调用父类的构造器
        super(n);
        System.out.println("一参数Student\t"+n);
    }
  • A first sub-class constructor calls this constructor overload class B with this, B and then call the parent class constructor.
//父类
 Person(String name){
        System.out.println("Person()\t"+name);
    }
//子类
Student(String n){
    //super关键字调用父类的构造器
        super(n);
        System.out.println("一参数Student\t"+n);
    }
Student(String n,String m){
//this关键字调用同一类中重载的构造器
    this(n);
    System.out.println("二参数student()\t"+m);
}
  • When the sub-class constructor is not super and this, the system will implicitly call the parent class's constructor with no arguments, no arguments if not, then an error.
//隐式调用父类构无参数构造器,但是父类没有,所以要用super显式调用
PrimaryStudent(){
//没有下面的语句会报错
    super("hello");
    System.out.println("PrimaryStudent()");
}

To sum up :

When you call a subclass subclass constructor to initialize the object, the parent class constructor will always be executed before the subclass constructor . Even, the parent class of the parent class will be executed before the parent class ...... the way back to the superclass constructor Object class of all classes.

Reference books: "Thinking in Java", "crazy Java handouts", "Java core technology Volume I"

Guess you like

Origin www.cnblogs.com/summerday152/p/12041632.html