Java核心(七):this和super的区别

一、this
  this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针。
  this的用法在java中大体可以分为3种:

  1、普通的直接引用

  this相当于是指向当前对象本身。

  2、形参与成员名字重名,用this来区分:

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

  3.引用本类的构造函数

class Person{
    private String name;
    private int age;
    
    public Person() {
    }
 
    public Person(String name) {
        this.name = name;
    }
    public Person(String name, int age) {
        this(name);
        this.age = age;
    }
}

二、super

  super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类。
  super也有三种用法:
  1、普通的直接引用
  与this类似,super相当于是指向当前对象的父类的引用,这样就可以用super.xxx来引用父类的成员。
  2、子类中的成员变量或方法与父类中的成员变量或方法同名时,用super进行区分
class Person{
    protected String name;
 
    public Person(String name) {
        this.name = name;
    }
 
}
 
class Student extends Person{
    private String name;
 
    public Student(String name, String name1) {
        super(name);
        this.name = name1;
    }
 
    public void getInfo(){
        System.out.println(this.name);      //Child
        System.out.println(super.name);     //Father
    }
 
}
public class Test {
 
    public static void main(String[] args) {
       Student s1 = new Student("Father","Child");
       s1.getInfo();
 
    }
 
}

  3、引用父类构造函数

  super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句)。
  this(参数):调用本类中另一种形式的构造函数(应该为构造函数中的第一条语句)。
 
三、super和this的异同:
  • super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名    super.成员函数据名(实参)
  • this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名)
  • super()和this()类似,区别是,super()在子类中调用父类的构造方法,this()在本类内调用本类的其它构造方法。
  • super()和this()均需放在构造方法内第一行。
  • 尽管可以用this调用一个构造器,但却不能调用两个。
  • this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
  • this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
  • 从本质上讲,this是一个指向本对象的指针, 然而super是一个Java关键字。
posted @ 2019-02-26 15:19  荆小六  阅读(9989)  评论(1编辑  收藏  举报