Java this關(guān)鍵字的使用詳解
1. 先看一段代碼,并分析問題
public class This01 { //編寫一個main方法 public static void main(String[] args) { Dog dog1 = new Dog("大壯", 3); //dog1調(diào)用了 info()方法 dog1.info(); } } class Dog{ //類 String name; int age; // public Dog(String dName, int dAge){//構(gòu)造器 // name = dName; // age = dAge; // } //如果我們構(gòu)造器的形參,能夠直接寫成屬性名,就更好了 //但是出現(xiàn)了一個問題,根據(jù)變量的作用域原則 //構(gòu)造器的name 是局部變量,而不是屬性 //構(gòu)造器的age 是局部變量,而不是屬性 //==> 引出this關(guān)鍵字來解決 public Dog(String name, int age){//構(gòu)造器 //this.name 就是當前對象的屬性name this.name = name; //this.age 就是當前對象的屬性age this.age = age; } public void info(){//成員方法,輸出屬性x信息 System.out.println(name + "\t" + age + "\t"); } }
2. 深入理解 this
為了進一步理解 this,我們再看一個案例 (This01.java)
使用hashCode()
看看對象的情況
public class This01 { //編寫一個main方法 public static void main(String[] args) { Dog dog1 = new Dog("大壯", 3); System.out.println("dog1的hashcode=" + dog1.hashCode()); //dog1調(diào)用了 info()方法 dog1.info(); System.out.println("============"); Dog dog2 = new Dog("大黃", 2); System.out.println("dog2的hashcode=" + dog2.hashCode()); dog2.info(); } } class Dog{ //類 String name; int age; public Dog(String name, int age){//構(gòu)造器 //this.name 就是當前對象的屬性name this.name = name; //this.age 就是當前對象的屬性age this.age = age; System.out.println("this.hashCode=" + this.hashCode()); } public void info(){//成員方法,輸出屬性x信息 System.out.println("this.hashCode=" + this.hashCode()); System.out.println(name + "\t" + age + "\t"); } }
3. this 的注意事項和使用細節(jié)
ThisDetail.java
- this 關(guān)鍵字可以用來訪問本類的屬性、方法、構(gòu)造器
- this 用于區(qū)分當前類的屬性和局部變量
public class ThisDetail { public static void main(String[] args) { T t = new T(); t.f3(); } } class T{ String name = "兮動人"; int num = 10; //this關(guān)鍵字可以用來訪問本類的屬性 public void f3(){ String name = "smith"; //傳統(tǒng)方式 System.out.println("name=" + name + " num=" + num);//smith 100 //也可以使用this訪問屬性 System.out.println("name=" + this.name + " num=" + this.num);//jack 100 } }
訪問成員方法的語法:this.方法名(參數(shù)列表);
public class ThisDetail { public static void main(String[] args) { T t1 = new T(); t.f2(); } } class T { public void f1(){ System.out.println("f1()方法..."); } public void f2(){ System.out.println("f2()方法..."); //調(diào)用本類的 f1 //第一種方式 f1(); //第二種方式 this.f1(); } }
訪問構(gòu)造器語法:this(參數(shù)列表);
注意只能在構(gòu)造器中使用(即只能在構(gòu)造器中訪問另外一個構(gòu)造器, 必須放在第一條語句)
public class ThisDetail { public static void main(String[] args) { T t2 = new T(); } } class T{ /* 細節(jié): 訪問構(gòu)造器語法:this(參數(shù)列表); 注意只能在構(gòu)造器中使用(即只能在構(gòu)造器中訪問另外一個構(gòu)造器) 注意: 訪問構(gòu)造器語法:this(參數(shù)列表); 必須放置第一條語句 */ public T(){ //這里去訪問 T(String name,int age)構(gòu)造器,必須放在第一行 this("Jack", 23); System.out.println("T()構(gòu)造器"); } public T(String name,int age){ System.out.println("T(String name,int age)構(gòu)造器"); } }
this 不能在類定義的外部使用,只能在類定義的方法中使用。
4. this 的案例
TestPerson.java
定義 Person 類,里面有 name、age 屬性,并提供 compareTo 比較方法,用于判斷是否和另一個人相等,提供測試類 TestPerson 用于測試, 名字和年齡完全一樣,就返回 true, 否則返回 false
public class TestPerson { //編寫一個main方法 public static void main(String[] args) { Person p1 = new Person("mary", 20); Person p2 = new Person("mary", 20); System.out.println("p1和p2比較的結(jié)果=" + p1.compareTo(p2)); } } /* 定義Person類,里面有name、age屬性,并提供compareTo比較方法, 用于判斷是否和另一個人相等,提供測試類TestPerson用于測試, 名字和年齡完全一樣,就返回true, 否則返回false */ class Person { String name; int age; //構(gòu)造器 public Person(String name, int age) { this.name = name; this.age = age; } //compareTo比較方法 public boolean compareTo(Person p) { //名字和年齡完全一樣 // if(this.name.equals(p.name) && this.age == p.age) { // return true; // } else { // return false; // } return this.name.equals(p.name) && this.age == p.age; } }
把名字或年齡改成其他不同數(shù)據(jù)
到此這篇關(guān)于Java this關(guān)鍵字的使用詳解的文章就介紹到這了,更多相關(guān)Java this內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?SpringBoot整合shiro-spring-boot-starterqi項目報錯解決
這篇文章主要介紹了Java?SpringBoot整合shiro-spring-boot-starterqi項目報錯解決,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考一下2022-08-08MyBatis數(shù)據(jù)脫敏的實現(xiàn)方案介紹
在我們數(shù)據(jù)庫中有些時候會保存一些用戶的敏感信息,比如:手機號、銀行卡等信息,如果這些信息以明文的方式保存,那么是不安全的2022-08-08SpringBoot定時任務(wù)不執(zhí)行的幾個可能原因及解決方法
這篇文章主要介紹了SpringBoot定時任務(wù)不執(zhí)行的幾個可能原因及解決方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12Mybatis分頁插件Pagehelper的PageInfo字段屬性使用及解釋
這篇文章主要介紹了Mybatis分頁插件Pagehelper的PageInfo字段屬性使用及解釋,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Java創(chuàng)建,編輯與刪除Excel迷你圖表的實現(xiàn)方法
迷你圖是Excel工作表單元格中表示數(shù)據(jù)的微型圖表。本文將通過Java代碼示例介紹如何在Excel中創(chuàng)建迷你圖表,以及編輯和刪除表格中的迷你圖表,需要的可以參考一下2022-05-05Spring超出最大會話數(shù)(Max?sessions?limit?reached:?10000)
在Spring系統(tǒng)中遇到的Maxsessionslimitreached:10000錯誤,該錯誤由于會話數(shù)超過默認限制10000而觸發(fā),下面就來介紹一下解決方法,感興趣的可以了解一下2024-12-12