013-Keyword super

Disclaimer: All my articles are the compilation of online teaching videos, including Mad God Talk, Shang Silicon Valley, Dark Horse Programmer, etc., used as reference materials, without any commercial use, please refer to the majority of netizens, please do not spray ,Thank you. (Note that due to the website, some code characters may have problems. It is recommended that when reading the code, it is best to look
at the corresponding picture below) 1. Use super in the Java class to call the specified operation in the parent class
1. super can be used to access the properties defined in the parent class
2. super can be used to call the member methods defined in the parent class
3. super can be used to call the constructor of the parent class in the subclass constructor.
Note:
1. When the child parent class has the same name When you are a member, you can use super to indicate that you are calling a member of the parent class.
2. The traceability of super is not limited to the direct parent class.
3. The usage of super and this is similar. This represents the reference of the object of this class, and super represents the memory space of the parent class. Logo
example:

class Person {
protected String name = “张三”;
protected int age;
public String getInfo() {
return “Name:” + name + “\nage:” + age;
}
}
class Student extends Person {
protected String name = “李四”;
private String school = “New Oriental”;
public String getSchool() {
return school;
}
public String getInfo() {
return super.getInfo() + “\nschool:” + school;
}
}
public class StudentTest {
public static void main(String[] args) {
Student st = new Student();
System.out.println(st.getInfo());
}
}

Second, call the constructor of the parent class
1. All constructors in the subclass will access the constructor of the empty parameter in the parent class by default.
2. When there is no constructor with the empty parameter in the parent class, the constructor of the subclass must pass this( The parameter list) or super (parameter list) statement specifies to call the corresponding constructor in this class or parent class. At the same time, you can only "choose one of two", and it must be placed in the first line of the constructor.
3. If the subclass constructor neither explicitly calls the parent class or the constructor of this class, and there is no parameterless in the parent class Constructor, the compilation error
example:

class Person {
private String name;
private int age;
private Date birthDate;

public Person(String name,int age,Date d) {
this.name = name;
this.age = age;
this.birthDate = d;
}
public Person(String name,int age) {
this(name,age,null);
}
public Person(String name,Date d) {
this(name,30,d);
}
public Person(String name) {
this(name,30);
}
}

public class Student extends Person {
private String school;
public Student(String name,int age,String s) {
super(name,age);
school = s;
}
public Student(String name,String s) {
super(name);
school = s;
}
//Compile error: no super(), the system will call the parent class constructor
public Student(String s) {
school = s;
}
}

Three, the difference between this and super
013-Keyword super

Guess you like

Origin blog.51cto.com/12859164/2552111