java super - 总结
1.super 关键字可以理解为:父类的
2.可以用来调用的结构:
- 父类的:属性、方法、构造器
3.super调用属性、方法:
- 3.1 我们可以在子类的方法或构造器中。通过使用"super.属性"或"super.方法"的方式,显式的调用父类中声明的属性或方法。但是,通常情况下,我们习惯省略"super."
- 3.2 特殊情况:当子类和父类中定义了同名的属性时,我们要想在子类中调用父类中声明的属性,则必须显式的使用"super.属性"的方式,表明调用的是父类中声明的属性。
- 3.3 特殊情况:当子类重写了父类中的方法以后,我们想在子类的方法中调用父类中被重写的方法时,则必须显式的使用"super.方法"的方式,表明调用的是父类中被重写的方法。
4.super调用构造器:
- 4.1 我们可以在子类的构造器中显式的使用"super(形参列表)"的方式,调用父类中声明的指定的构造器
- 4.2 "super(形参列表)"的使用,必须声明在子类构造器的首行!
- 4.3 我们在类的构造器中,针对于"this(形参列表)"或"super(形参列表)"只能二选一,不能同时出现
- 4.4 在构造器的首行,没显式的声明"this(形参列表)“或"super(形参列表)”,则默认调用的是父类中空参的构造器:super()
- 4.5 在类的多个构造器中,父类没有提供无参构造器。子类至少一个构造器中使用了"super(形参列表)",调用父类中的构造器
5.super调用构造器demo
5.1. Demo类介绍
demo有三个类,父类、子类、测试类。
- 父类:Person
package tepmdemo.extenddemo;
import javax.jnlp.PersistenceService;
/**
* @author bruce
* @create 2020/4/5 17:41
*/
public class Person {
protected String name;
protected String sex;
protected Person() {
System.out.println("Person无参构造器");
}
protected Person(String name) {
this.name = name;
System.out.println("Person.name");
}
protected Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public void eat() {
System.out.println("Person.eat");
}
}
- 子类:Student
package tepmdemo.extenddemo;
import day04.generic.Stu;
/**
* @author bruce
* @create 2020/4/5 17:46
*/
public class Student extends Person {
public Student(String name) {
this.name = name;
}
}
- 测试类 App
package tepmdemo.extenddemo;
/**
* @author bruce
* @create 2020/4/5 17:49
*/
public class App {
public static void main(String[] args) {
Student student = new Student("zhanxin");
student.eat();
}
}
5.2. 当父类有多个构造器,子类默认调用父类的无参构造器。
4.4在构造器的首行,没显式的声明"this(形参列表)“或"super(形参列表)”,则默认调用的是父类中空参的构造器:super()
- 子类:初始化子类有参构造器
public class Student extends Person {
public Student(String name) {
this.name = name;
}
}
- 测试类
public class App {
public static void main(String[] args) {
Student student = new Student("zhanxin");
}
}
- 测试结果打印出了父类的无参构造器
Person无参构造器
Process finished with exit code 0
5.3.父类有多个构造器,子类调用指定的父类构造器
4.1 我们可以在子类的构造器中显式的使用"super(形参列表)"的方式,调用父类中声明的指定的构造器
- 子类
public class Student extends Person {
public Student(String name) {
super(name);
this.name = name;
}
}
- 测试类
public class App {
public static void main(String[] args) {
Student student = new Student("zhanxin");
}
}
- 测试结果显示调用了父类的一个参数构造器
Person.name
Process finished with exit code 0
5.4. 父类没有无参构造器必须显示的调用父类的有参构造器
4.5 在类的多个构造器中,父类没有提供无参构造器。子类至少一个构造器中使用了"super(形参列表)",调用父类中的构造器
- 父类将无参构造器注释掉,不提供无参构造器
public class Person {
protected String name;
protected String sex;
/* protected Person() {
System.out.println("Person无参构造器");
}
*/
protected Person(String name) {
this.name = name;
System.out.println("Person.name");
}
protected Person(String name, String sex) {
this.name = name;
this.sex = sex;
System.out.println("Person两个参数构造器");
}
public void eat() {
System.out.println("Person.eat");
}
}
- 子类 构造器中必须用super参数列表显示调用父类的有参构造器
public class Student extends Person {
public Student(String name,String sex) {
//显示调用父类的有参构造器
super(name, sex);
this.name = name;
}
}
- 测试类
public class App {
public static void main(String[] args) {
Student student = new Student("张欣","女");
}
}
- 测试结果 显示调用了父类的两个参数构造器
Person两个参数构造器
Process finished with exit code 0