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注意点

  1. super调用父类的构造方法,必须写在构造方法的第一行
  2. super必须只能出现在子类的方法或构造方法中
  3. super和this不能同时调用构造方法
posted @ 2022-09-13 15:37  7obui  阅读(44)  评论(0)    收藏  举报