本章目标:
this是什么?
this是java中的关键字,可以把他当做一个变量看待,他就是一个引用,存储在jvm中的对象内部,每个java中的对象都有一个this。(如下图)
从图上可看出this指向的是当前对象,代表当前对象,Jack.name和this.name是一样的。
this能用在哪里?
实例方法中:
这里为什么只能在实例方法中呢?原因:很简单,this指向的是当前对象,如果是在static修饰的方法中根本就不需要当前对象的参与。
实例代码:
/**
* @author Jason
* @create 2020-04-21 11:02
*/
public class CustomerTest {
public static void main(String[] args) {
Customer jason = new Customer("jason");
System.out.println("main--->"+jason);
jason.shopping();
System.out.println("=========================");
Customer rose = new Customer("rose");
System.out.println("main--->"+rose);
rose.shopping();
}
}
/**
* @author Jason
* @create 2020-04-21 11:00
*/
public class Customer {
private String name ;
public Customer() {
}
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void shopping(){
System.out.println("shopping()--->"+this);
}
}
输出结果:
main--->com.jason.demo02.Customer@1540e19d
shopping()--->com.jason.demo02.Customer@1540e19d
=========================
main--->com.jason.demo02.Customer@677327b6
shopping()--->com.jason.demo02.Customer@677327b6
通过上面的实例可以发现:
this可以使用在实例方法当中,它指向当前正在执行这个动作的对象。 谁来调用this就指向谁。
构造方法中:
在构造方法的第一行用this关键字,通过当前的构造方法调用本类中的其他构造方法。以达到代码复用的效果。
代码实例:(这个是没有用this来调用其他构造方法的情况,可以看出代码还是非常的冗余,没有达到代码复用的效果)
/**
* @author Jason
* @create 2020-04-21 11:45
* 需求:默认创建日期为1970年1月1日
*/
public class Date {
private int year;
private int month;
private int day;
public Date() {
this.year=1970;
this.month=01;
this.day=01;
}
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
/**
* @author Jason
* @create 2020-04-21 11:49
*/
public class DateTest {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println(d1.getYear()+"年"+d1.getMonth()+"月"+d1.getDay()+"日");
Date d2 = new Date(2020,02,20);
System.out.println(d2.getYear()+"年"+d2.getMonth()+"月"+d2.getDay()+"日");
}
}
对上面代码实例的改进,其他代码不用动,只是将无参数构造方法中的代码进行改动就行,用this关键字来调用本类当中有参数的构造方法,然后将参数传递进去就可以了,这样就达到了复用有参数构造方法的目的。
具体实例:
/**
* @author Jason
* @create 2020-04-21 11:45
* 需求:默认创建日期为1970年1月1日
*/
public class Date {
private int year;
private int month;
private int day;
public Date() {
this(1970,01,01);
}
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
不能用在哪里?
this不能出现在static修饰的方法中,可以出现在实例方法中
this什么时候可以省略?
大部分地方都能省略
什么时候不能 省略?
在实例方法中区分局部变量和实例变量的时候就不能省略
怎么通过构造方法调用当前类中其它的构造方法?
在构造方法的第一行使用this调用其他构造方法就行