Java關鍵字this使用方法詳細講解(通俗易懂)
this關鍵字主要有以下三個地方使用
在方法體中引用當前對象,即其方法被調(diào)用的對象,以便將當前對象的實例變量或當前對象作為參數(shù)傳遞給其他方法。
① t = this.x; 要在方法中引用當前對象,可以使用關鍵字 this。
② return this; 作為當前方法的返回值等。
③ z.resetData(this); 關鍵字 this 指向當前對象,可用于任何可使用對象引用的地方:在句點表示法中,作為方法的參數(shù);
下面是一些使用 this 的例子,其中的注釋對相應的用法做了說明。
一、t = this.x;
this
用于指代當前對象,而 .x
則是該對象的屬性。因此,this.x
表示當前對象的屬性 x
public class MyClass { private int x; public MyClass(int x) { this.x = x; // 設置當前對象的屬性 x } public int getX() { return this.x; // 返回當前對象的屬性 x } public static void main(String[] args) { MyClass obj = new MyClass(10); int t = obj.getX(); // 使用 this.x 賦值給變量 t } }
還有在內(nèi)部類中,使用this關鍵字引用外部類對象。
public class OuterClass { private int value; public void outerMethod() { InnerClass inner = new InnerClass(); inner.innerMethod(); } public class InnerClass { public void innerMethod() { OuterClass.this.value = 10; // 使用this關鍵字引用外部類對象的成員變量 } } }
二、return this;
return this;
表示從當前方法中返回當前對象的引用,即返回指向當前對象的指針或引用。
當一個方法的返回類型是類本身(或該類的父類或接口),而不是基本數(shù)據(jù)類型或其他對象類型時,可以使用 return this;
來返回當前對象的引用。
例如,在一個類的方法中,如果你想要返回調(diào)用該方法的對象本身,而不是其他值,你可以使用 return this;
。這在鏈式調(diào)用或者需要返回當前對象的場景中特別有用。
public class Leaf { public static void main(String[] args){ Leaf leaf = new Leaf(); leaf.increment().increment().increment().print(); } int i = 0; Leaf increment(){ i++; return this; } void print(){ System.out.println("i = " + i); } }
increment
方法用于遞增實例變量i
的值并返回當前對象的引用,以便支持方法鏈的形式調(diào)用,實現(xiàn)了在一個語句中對同一個對象進行了多次操作。
三、Peeler.peel(this);
Apple 需要調(diào)用 Peeler.peel() 方法,它是一個外部的工具方法,為了將自身傳遞給外部方法,Apple必須使用 this 關鍵字。
/** * 這段代碼實現(xiàn)了一個簡單的場景,演示了對象間的方法調(diào)用和 toString 方法的使用。 * 在 main 方法中創(chuàng)建了一個 Person 對象,并調(diào)用其 eat 方法,傳入一個新創(chuàng)建的 Apple 對象。 * eat 方法內(nèi)部調(diào)用了傳入的 Apple 對象的 getPeeled 方法,獲取削皮后的蘋果對象。 * getPeeled 方法內(nèi)部調(diào)用了 Peeler 類的 peel 靜態(tài)方法,對當前蘋果對象進行削皮操作,并返回結(jié)果。 * 削皮操作完成后,削皮后的蘋果對象被打印輸出到控制臺。 */ public class PassingThis { public static void main(String[] args) { //在 main 方法中創(chuàng)建了一個 Person 對象,并調(diào)用其 eat 方法,傳入一個新創(chuàng)建的 Apple 對象。 new Person().eat(new Apple()); } } class Person { // Person 類中定義了一個 eat 方法,接受一個 Apple 對象作為參數(shù)。調(diào)用傳入的 Apple 對象的 getPeeled 方法,獲取削皮后的蘋果對象,然后打印輸出這個削皮后的蘋果對象。 public void eat(Apple apple) { Apple peeled = apple.getPeeled(); // 直接輸出 peeled 對象,會調(diào)用其 toString 方法 System.out.println("這個蘋果是:" + peeled); } } class Peeler { static Apple peel(Apple apple) { // Peeler 類中定義了一個靜態(tài)方法 peel,接受一個 Apple 對象作為參數(shù),并對這個蘋果對象進行削皮操作,削皮操作非常簡單,只是直接返回傳入的蘋果對象,沒有進行實際的削皮操作。 return apple; } } class Apple { Apple getPeeled() { // Apple 類中定義了一個 getPeeled 方法,它返回一個經(jīng)過削皮后的蘋果對象。 // 在這個方法中,通過調(diào)用 Peeler 類的 peel 靜態(tài)方法來實現(xiàn)對當前蘋果對象的削皮操作,并將結(jié)果返回。 return Peeler.peel(this); } @Override public String toString() { // 在 Apple 類中重寫了 toString 方法,使其返回一個描述削皮后蘋果的字符串,這樣在打印輸出時就會顯示你自定義的描述,而不是默認的類名和哈希碼。 return "削皮后的蘋果"; } }
注意:
this 關鍵字只能在方法內(nèi)部使用,表示對“調(diào)用方法的那個對象”的引用。同時,如果在方法內(nèi)部調(diào)用同一個類的同一個方法,就直接調(diào)用,不用加this。
public class Example { private int value; public Example(int value) { this.value = value; } // 方法內(nèi)部使用this關鍵字引用當前對象,并返回當前對象的值 public int getValue() { return this.value; } // 方法內(nèi)部調(diào)用同一個類的同一個方法,不需要加this public void printValue() { int val = getValue(); // 直接調(diào)用getValue方法 System.out.println("Value: " + val); } public static void main(String[] args) { Example example = new Example(10); example.printValue(); } }
這個示例中,Example類有一個私有成員變量value和兩個公共方法getValue和printValue。在getValue方法中,使用了this關鍵字來引用當前對象,并返回value的值。而在printValue方法中,直接調(diào)用了getValue方法,不需要加this關鍵字。在main方法中,創(chuàng)建了Example類的實例對象,并調(diào)用了printValue方法,輸出value的值。
參考:
總結(jié)
到此這篇關于Java關鍵字this使用方法的文章就介紹到這了,更多相關Java關鍵字this使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中byte、byte數(shù)組與int、long的轉(zhuǎn)換詳解
這篇文章分別給大家介紹了Java中byte和int之間的轉(zhuǎn)換、Java中 byte數(shù)組和int之間的轉(zhuǎn)換、Java中byte數(shù)組和long之間的轉(zhuǎn)換以及整理了整體工具類的源碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02Spring Data JPA 關鍵字Exists的用法說明
這篇文章主要介紹了Spring Data JPA 關鍵字Exists的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java中的使用及連接Redis數(shù)據(jù)庫(附源碼)
這篇文章主要介紹了Java中的使用及連接Redis數(shù)據(jù)庫(附源碼),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Spring?Security中如何獲取AuthenticationManager對象
有時需要使用AuthenticationManager(以下簡稱Manager)對象,可是這個對象不是Bean,沒有直接保存在Spring的Bean庫中,那么如何獲取Spring Security中的這個對象呢,需要的朋友可以參考下2022-11-11ConcurrentModificationException日志關鍵字報警思考分析
本文將記錄和分析日志中的ConcurrentModificationException關鍵字報警,還有一些我的思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-12-12SpringMVC九大組件之HandlerMapping詳解
這篇文章主要介紹了SpringMVC九大組件之HandlerMapping詳解,HandlerMapping 叫做處理器映射器,它的作用就是根據(jù)當前 request 找到對應的 Handler 和 Interceptor,然后封裝成一個 HandlerExecutionChain 對象返回,需要的朋友可以參考下2023-09-09Spring中的ThreadPoolTaskExecutor線程池使用詳解
這篇文章主要介紹了Spring中的ThreadPoolTaskExecutor線程池使用詳解,ThreadPoolTaskExecutor 是 Spring框架提供的一個線程池實現(xiàn),用于管理和執(zhí)行多線程任務,它是TaskExecutor接口的實現(xiàn),提供了在 Spring 應用程序中創(chuàng)建和配置線程池的便捷方式,需要的朋友可以參考下2024-01-01