在Java的学习与开发者我们经常遇到this和super关键字,那么它们的用法和区别是什么呢?

一、this关键字

1.this是什么?

this 是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针。

2.this的用法

this 的用法在 Java 中大体可以分为3种:

(1)普通的直接引用

最简单的用法,this 相当于是指向当前对象本身。

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

示例:

public class Animal {

public String name;

public int health;

public int love;

public Animal() {无参构造

}

public Animal(String name, int health, int love) {//有参构造方法

this.name = name;

this.health = health;

this.love = love;

}

}

在创建一个类,并且声明许多属性后,我们一般都要为这个类创建无参和有参构造方法,而在有参方法的参数列表中,我们会发现形参的名字与属性名相同,这时我们就需要用this关键字来区分,不然就会无法实现我们想要的效果。

(3)引用构造函数

注意:在一个构造方法中通过this关键字调用其它构造方法的时候,this关键字需要写在构造方法中的第一行位置。

示例:

public class ThisDemo01 {

public String name;

public String age;

public ThisDemo01() {

System.out.println("ThisDemo01类中的无参构造方法");

}

public ThisDemo01(String name, String age) {

//在一个构造方法中通过this关键字调用其它构造方法的时候,this关键字需要写在构造方法中的第一行位置

this();

System.out.println("ThisDemo01类中的有参构造方法");

//this();

this.name = name;

this.age = age;

}

public static void main(String[] args) {

ThisDemo01 thisDemo01 = new ThisDemo01();

System.out.println("---------------");

ThisDemo01 thisDemo012 = new ThisDemo01("张三", "22");

}

}

结果:

 二、super关键字

1.super是什么?

super 可以理解为是指向自己父(根、超)类对象的一个指针,而这个父类指的是离自己最近的一个父类。

2.super的用法

super 也有三种用法:

(1)普通的直接引用

与 this 类似,super 相当于是指向当前对象的父类,这样就可以用 super.xxx 来引用父类的成员。

(2)子类中的成员变量或方法与父类中的成员变量或方法同名:

class Country {

String name;

void value() {

name = "China";

}

}

class City extends Country {

String name;

void value() {

name = "Shanghai";

super.value(); //调用父类的方法

System.out.println(name);

System.out.println(super.name);

}

public static void main(String[] args) {

City c=new City();

c.value();

}

}

结果:

Shanghai

China

可以看到,这里既调用了父类的方法,也调用了父类的变量。若不调用父类方法 value(),只调用父类变量 name 的话,则父类 name 值为默认值 null。

(3)引用构造函数

super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句)。

//父类:

public class Animal{

private String name;

private int health;

private int love;

public Animal() {

}

public Animal(String name, int health, int love) {

this.name = name;

this.health = health;

this.love = love;

}

//子类:

public class Dog extends Animal{

// 品种

private String strain;

public Dog() {

}

public Dog(String name, int health, int love, String strain) {

super(name, health, love);

this.strain = strain;

}

由上述代码可以看到,我们可以在子类的构造方法中用super关键字调用父类具有同样参数的构造方法,减少代码量。

(4)super的一些限制:

 super只能出现在子类的方法和构造方法中 ;

super调用构造方法时,只能是第一句 ;

super不能访问父类的private成员;

this() 和 super() 都指的是对象,所以,均不可以在 static 环境中使用。包括:static 变量,static 方法,static 语句块。 

三、super和this关键字的异同

(1)super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句)

(2)this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)

(3)super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名 super.成员函数据名(实参) this:它代表当前对象名(在程序中易产生二义性之处,应使用 this 来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用 this 来指明成员变量名)

(4)调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用 super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。

(5)super() 和 this() 类似,区别是,super() 从子类中调用父类的构造方法,this() 在同一类内调用其它方法。

(6)super() 和 this() 均需放在构造方法内第一行。

(7)尽管可以用this调用一个构造器,但却不能调用两个。

(8)this 和 super 不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有 super 语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。

(9)this() 和 super() 都指的是对象,所以,均不可以在 static 环境中使用。包括:static 变量,static 方法,static 语句块。

(10)从本质上讲,this 是一个指向本对象的指针, 然而 super 是一个 Java 关键字。

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: