继承

继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模

extends 扩展。子类是父类的扩展

Java中只有单继承,没有多继承。

 

publlic class Preson(){
   public void say(){
       System.out.println("说了一句话");
  }
}

public class Student extends Person{
   
}

public static void main(String[] args){
   Student s1 = new Studnet();
   student.say();
   
}
//Java中,所有的类,都默认直接或间接继承Object类

1.super

publlic class Preson(){
   
   public Preson(){
        System.out.println("Person被调用了");
  }
   
   protectded Sring name="aaaa";
   
    public void print(){
        System.out.println("Person");
  }
}

public class Student extends Person{
   //隐藏代码:调用了父类的无参构造
     public Student(){
         super();
        System.out.println("Student被调用了");
  }
   
   private Sring name="bbbbb";

   public void print(){
        System.out.println("Student");
  }
   public void test1(){
       print();//Student
       this.print();//Student
       super.print();//Preson
  }
}

public static void main(String[] args){
   Student s1 = new Studnet();
   
   s1.test1();
   
}

/*
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中
3.super和this不能同时调用构造方法

this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提
this:没有继承也可以使用
super:只有在继承条件才可以使用
构造方法:
this();本类的构造
super();父类的构造

*/

 

posted @ 2021-07-26 19:19  清钦  阅读(238)  评论(0)    收藏  举报