Java中this關(guān)鍵字的用法詳解
一、this關(guān)鍵字
1.this的類型:哪個(gè)對(duì)象調(diào)用就是哪個(gè)對(duì)象的引用類型

二、用法總結(jié)
1.this.data; //訪問屬性
2.this.func(); //訪問方法
3.this(); //調(diào)用本類中其他構(gòu)造方法
三、解釋用法
1.this.data
這種是在成員方法中使用
讓我們來看看不加this會(huì)出現(xiàn)什么樣的狀況
class MyDate{
public int year;
public int month;
public int day;
public void setDate(int year, int month,int day){
year = year;//這里沒有加this
month = month;//這里沒有加this
day = day;//這里沒有加this
}
public void PrintDate(){
System.out.println(year+"年 "+month+"月 "+day+"日 ");
}
}
public class TestDemo {
public static void main(String[] args) {
MyDate myDate = new MyDate();
myDate.setDate(2000,9,25);
myDate.PrintDate();
MyDate myDate1 = new MyDate();
myDate1.setDate(2002,7,14);
myDate1.PrintDate();
}
}我們想要達(dá)到的預(yù)期是分別輸出2000年9月25日,2002年7月14日。
而實(shí)際輸出的結(jié)果是

而當(dāng)我們加上this時(shí)
class MyDate{
public int year;
public int month;
public int day;
public void setDate(int year, int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void PrintDate(){
System.out.println(this.year+"年 "+this.month+"月 "+this.day+"日 ");
}
}
public class TestDemo {
public static void main(String[] args) {
MyDate myDate = new MyDate();
myDate.setDate(2000,9,25);
myDate.PrintDate();
MyDate myDate1 = new MyDate();
myDate1.setDate(2002,7,14);
myDate1.PrintDate();
}
}
就實(shí)現(xiàn)了賦值的功能,為了避免出現(xiàn)差錯(cuò),我們建議盡量帶上this
2.this.func()
這種是指在普通成員方法中使用this調(diào)用另一個(gè)成員方法
class Student{
public String name;
public void doClass(){
System.out.println(name+"上課");
this.doHomeWork();
}
public void doHomeWork(){
System.out.println(name+"正在寫作業(yè)");
}
}
public class TestDemo2 {
public static void main(String[] args) {
Student student = new Student();
student.name = "小明";
student.doClass();
}
}運(yùn)行結(jié)果:

3.this()
這種指在構(gòu)造方法中使用this調(diào)用本類其他的構(gòu)造方法
這種this的使用注意以下幾點(diǎn)
1.this只能在構(gòu)造方法中調(diào)用其他構(gòu)造方法
2.this要放在第一行
3.一個(gè)構(gòu)造方法中只能調(diào)用一個(gè)構(gòu)造方法


運(yùn)行結(jié)果

總結(jié)
到此這篇關(guān)于Java中this關(guān)鍵字用法的文章就介紹到這了,更多相關(guān)Java this關(guān)鍵字用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java for循環(huán)常見優(yōu)化方法案例詳解
這篇文章主要介紹了Java for循環(huán)常見優(yōu)化方法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
JDK14的新特性NullPointerExceptions的使用
這篇文章主要介紹了JDK14的新特性NullPointerExceptions的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Java項(xiàng)目中Service 層不直接返回Result 對(duì)象的原因分析
在Service層直接返回Result對(duì)象會(huì)導(dǎo)致業(yè)務(wù)邏輯與表現(xiàn)邏輯耦合,降低代碼清晰度和可維護(hù)性,正確的做法是讓每一層專注于自己的職責(zé),保持代碼可復(fù)用性,這篇文章給大家介紹為什么Java里面,Service 層不直接返回Result對(duì)象,感興趣的朋友跟隨小編一起看看吧2026-02-02
Java基礎(chǔ)之重載(Overload)與重寫(Override)詳解
這篇文章主要介紹了Java基礎(chǔ)之重載(Overload)與重寫(Override)詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
基于SpringBoot+Tess4j實(shí)現(xiàn)圖像文字識(shí)別功能
文章介紹了Tesseract?OCR技術(shù)及其Java封裝庫(kù)Tess4j,并詳細(xì)描述了如何在SpringBoot項(xiàng)目中集成和使用Tess4j進(jìn)行圖像識(shí)別,文章涵蓋了下載字體庫(kù)、配置SpringBoot項(xiàng)目、編寫測(cè)試代碼及圖片識(shí)別測(cè)試等步驟,并提供了中文、英文及中英混合識(shí)別的實(shí)戰(zhàn)示例,需要的朋友可以參考下2026-04-04
SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Java中使用Preferences 的 API設(shè)置用戶偏好
這篇文章主要介紹了Java中使用Preferences 的 API設(shè)置用戶偏好的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
ShardingSphere數(shù)據(jù)庫(kù)讀寫分離算法及測(cè)試示例詳解
這篇文章主要為大家介紹了ShardingSphere數(shù)據(jù)庫(kù)讀寫分離算法及測(cè)試示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

