this是指本類的當(dāng)前對象,而super是指本類的父類的當(dāng)前對象;使用this調(diào)用本類的屬性和方法,而使用super調(diào)用父類的屬性和方法。
當(dāng)本類中沒有覆蓋父類的屬性或者方法時,可以不使用super關(guān)鍵字就可以調(diào)用父類的屬性或者方法;需要注意的是子類調(diào)用構(gòu)造函數(shù)的時候,需要先調(diào)用父類的構(gòu)造函數(shù),然后在調(diào)用本類的構(gòu)造方法(因此需要在本類的構(gòu)造函數(shù)里面的第一行寫上super(),這個方法里面的參數(shù)可以有也可以沒有,根據(jù)需要而定)。

下面附個小程序:

class People{
 private String name;
 private int age;
 
 public People(){
  System.out.println("先調(diào)用父類無參數(shù)構(gòu)造方法");
 }
 public People(String n){
  System.out.println("先調(diào)用父類構(gòu)造方法");
 }
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 //set人的屬性
 public void setInfor(String name,int age){
  this.setName(name);
  this.setAge(age);
 }
 //get人的屬性
 public void getInfor(){
  System.out.print("姓名:"+this.getName()+"   年齡:"+this.getAge());
 }
}


class Student extends People{
 private String school;
 
 public Student(){
  super("a");
  System.out.println("后調(diào)用學(xué)生構(gòu)造方法");
 }
 
 public String getSchool() {
  return school;
 }
 public void setSchool(String school) {
  this.school = school;
 }
 //set學(xué)生的屬性
 public void setInfor(String name,int age,String school){
  super.setInfor(name, age);
  this.setSchool(school);
 }
 //get學(xué)生的屬性
 public void getInfor(){
  super.getInfor();
  System.out.println("  學(xué)校:"+school);
 }
 
}


public class Extends {
 public static void main(String [] args){
  Student student = new Student();
  student.setInfor("王倩",23,"河北理工");
  student.getInfor();
  
 }

}


需要特別注意的是:子類調(diào)用構(gòu)造函數(shù)的時候,需要先調(diào)用父類構(gòu)造函數(shù),因此要把super()寫在構(gòu)造函數(shù)里面的第一行;子類的setInfor方法和getInfor方法都調(diào)用了父類的相對應(yīng)的方法。

下面看看運(yùn)行結(jié)果: