super

Posted on 2023-03-25 13:02  离001  阅读(27)  评论(0)    收藏  举报

super

public class example {
   public static void main(String[] args) {
       Student student = new Student();
       student.test("qinjiang");
       student.test1();



  }
}

父类

public class Person {



   protected String name = "mengquan";
   public void print(){
       System.out.println("Person");
  }

}

子类

public class Student extends Person{
   private String name = "sz";


   public void print(){
       System.out.println("Student");
  }


   public void test(){
       print();
       this.print();
       super.print();
  }

   

}

 

super关键字的调用

即是子类调用父类的方法

 

注意点

1.super调用父类的构造方法,必须在构造方法的第一个

2.super必须只能出现在子类的方法或者构方法中

3.super和this不能同时调用构造方法

 

Vs this:

代表对象不同:

this: 本身调用这个对象

super:代表父类对象的应用

前提

this:没有继承也可以使用

super:只能在继承条件下才可以使用

构造方法

this():本类的构造

super():父类的构造