super详解
super详解
public class Person {
protected String name = "per";
}
public class Student extends Person{
private String name = "stu";
public void test(String name){
System.out.println(name); // 调用自己的 stu
System.out.println(this.name); // 调用自己的 stu
System.out.println(super.name); // 调用父类的 per
}
}
Student s = new Student();
s.test();
子类无参构造函数调用时,先调用父类的无参构造函数
super注意点
- super调用父类的构造方法,必须写在构造方法的第一行
- super必须只能出现在子类的方法或构造方法中
- super和this不能同时调用构造方法
浙公网安备 33010602011771号