Super类
//主程序中
public class Application {
static void main(String[] args) {
Student student = new Student();
System.out.println("===============");
student.test("xm");
System.out.println("===============");
student.test1();
}
}
//父类中
public class Person {
protected String name="daming";
public void print(){
System.out.println("Person print");
}
public Person(){
System.out.println("Person无参执行");
}
}
//子类中
public class Student extends Person {
public String name="xiaoming";
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
public void print(){
System.out.println("Student print");
}
public void test1(){
print();//当前类的print
this.print();
super.print();//若父类的对应方法为私有private,则无法调用
}
public Student(){
//暗藏super(); 父类的无参构造,且必须写于首行,率先执行
//若父类没有无参构造,则子类无法存在无参构造
System.out.println("Student无参执行");
}
}
super注意点
- super调用父类的构造方法,必须在构造方法的第一个
- super只能出现在子类的方法或构造器中,否则将调用object类
- super与this无法同时调用构造器(二者都需写于首行率先执行)
相较于this
- 代表对象不同:
this: 本身调用者这个对象
super: 代表父类对象的应用 - 前提:
this:没进行继承也能使用
super:只能在继承条件下使用 - 构造方法:
this(); 用于本类构造
super(); 用于父类构造

浙公网安备 33010602011771号