本章目標:
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調用其他構造方法就行