super

super

package se.oop;

public class Super {
  public static void main(String[] args) {
      //super关键字:调用父类
      //private的东西无法被继承
  /*
  super注意点
  -super调用父类的构造方法,必须在构造方法的第一个
  -super只能出现在子类的方法或者构造方法中
  -super和this不能同时调用构造方法

  super和this的区别
  1.代表的对象不同
  -this:当前类的这个对象
  -super:代表父类对象的引用

  2.调用的前提条件不同
  -this:没有extends也可以使用
  -super:只能在extends条件下才可以使用

  3.调用的构造方法不同
  -this:当前类的构造
  -super:父类的构造
    */

  /*
  public class AppLication {
  public static void main(String[] args) {
      Studyent studyent = new Studyent();
      studyent.test("贺");
      studyent.test1();

  父类:
      public Person() {
      System.out.println("Person无参构造执行了");
      }

  protected:受保护的属性
  protected String name = "heqianfa";

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

  子类:
      public Studyent() {
      //父类的无参构造先执行,有段隐藏代码super()先调用了父类的无参
      super();//调用父类的构造器必须在子类构造器的第一行
      System.out.println("Studyent无参构造执行了");

      private String name = "fazai";

  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("Stundyend");
  }
  public void test1(){//调用方法
      print();
      this.print();
      super.print();
      }  
*/

  }
}
posted @ 2021-10-11 23:41  fantasyfa  阅读(73)  评论(0)    收藏  举报