Java培训教程之this关键字讲解
前面小節(jié)中,類在定義成員方法時(shí),局部變量和成員變量可以重名,但此時(shí)不能訪問成員變量。為避免這種情形,Java提供了this關(guān)鍵字,表示當(dāng)前對(duì)象,指向調(diào)用的對(duì)象本身。接下來(lái)演示this的本質(zhì),如例4-1所示。
例4-1 TestThis.java
1 class Person {
2 public void equals(Person p) {
3 System.out.println(this); // 打印this的地址
4 System.out.println§; // 打印對(duì)象地址
5 if (this == p) // 判斷當(dāng)前對(duì)象與this是否相等
6 System.out.println(“相等”);
7 else
8 System.out.println(“不相等”);
9 }
10 }
11 public class TestThis {
12 public static void main(String[] args) {
13 Person p1 = new Person();
14 Person p2 = new Person();
15 p1.equals(p1);
16 p1.equals(p2);
17 }
18 }
程序的運(yùn)行結(jié)果如圖4.1所示。
圖4.1 例4-1運(yùn)行結(jié)果
在圖4.1中,從程序運(yùn)行結(jié)果可發(fā)現(xiàn),關(guān)鍵字this和調(diào)用對(duì)象p1的值相等,都保存了指向堆內(nèi)存空間的地址,也就是說(shuō),this就是調(diào)用對(duì)象本身。因此,調(diào)用對(duì)象p1的this與p2對(duì)象不相等。
this關(guān)鍵字在程序中主要有三種用法,下面來(lái)分別講解各種用法:
1.使用this調(diào)用類中的屬性
this關(guān)鍵字可以明確調(diào)用類的成員變量,不會(huì)與局部變量名發(fā)生沖突。接下來(lái)演示this調(diào)用屬性,如例4-2所示。
例4-2 TestThisRefAttr.java
1 class Person {
2 private String name; // 聲明姓名私有屬性
3 private int age; // 聲明年齡私有屬性
4 public Person(String name, int age) {
5 this.name = name; // 明確表示為類中的name屬性賦值
6 this.age = age; // 明確表示為類中的age屬性賦值
7 }
8 public void say() { // 定義顯示信息的方法
9 System.out.println(“姓名:”+this.name+",年齡:"+this.age);
10 }
11 }
12 public class TestThisRefAttr {
13 public static void main(String[] args) {
14 Person p = new Person(“張三”, 18);
15 p.say();
16 }
17 }
程序的運(yùn)行結(jié)果如圖4.2所示。
圖4.2 例4-2運(yùn)行結(jié)果
例4-2中,構(gòu)造方法的形參與成員變量同名,使用this明確調(diào)用成員變量,避免了與局部變量產(chǎn)生沖突。
2.使用this調(diào)用成員方法
this既然可以訪問成員變量,那么也可以訪問成員方法,如例4-3所示。
例4-3 TestThisRefFun.java
1 class Person {
2 private String name; // 聲明姓名私有屬性
3 private int age; // 聲明年齡私有屬性
4 public Person(String name, int age) {
5 this.name = name; // 明確表示為類中的name屬性賦值
6 this.age = age; // 明確表示為類中的age屬性賦值
7 }
8 public void say() { // 定義顯示信息的方法
9 System.out.println(“姓名:”+this.name+",年齡:"+this.age);
10 this.log(“Person.say”); // this調(diào)用成員方法
11 }
12 public void log(String msg) {
13 System.out.println(“日志記錄:調(diào)用”+msg);
14 }
15 }
16 public class TestThisRefFun {
17 public static void main(String[] args) {
18 Person p = new Person(“張三”, 18);
19 p.say();
20 }
21 }
程序的運(yùn)行結(jié)果如圖4.3所示。
圖4.3 例4-3運(yùn)行結(jié)果
例4-3中,在say()方法中明確使用this調(diào)用log()成員方法。另外,此處的this可以省略,但建議不要省略,使代碼更加清晰。
3.使用this調(diào)用構(gòu)造方法
構(gòu)造方法是在實(shí)例化時(shí)被自動(dòng)調(diào)用的,因此不能直接像調(diào)用成員方法一樣去調(diào)用構(gòu)造方法,但可以使用this([實(shí)參列表])的方式調(diào)用其他的構(gòu)造方法,如例4-4所示。
例4-4 TestThisRefConstructor.java
1 class Person {
2 private String name; // 聲明姓名私有屬性
3 private int age; // 聲明年齡私有屬性
4
5 public Person() {
6 System.out.println(“調(diào)用無(wú)參構(gòu)造方法”);
7 }
8 public Person(String name, int age) {
9 this(); // 調(diào)用無(wú)參構(gòu)造函數(shù)
10 System.out.println(“調(diào)用有參構(gòu)造函數(shù)”);
11 this.name = name; // 明確表示為類中的name屬性賦值
12 this.age = age; // 明確表示為類中的age屬性賦值
13 }
14 public void say() { // 定義顯示信息的方法
15 System.out.println(“姓名:”+this.name+",年齡:"+this.age);
16 }
17 }
18 public class TestThisRefConstructor {
19 public static void main(String[] args) {
20 Person p = new Person(“張三”, 18);
21 p.say();
22 }
23 }
程序的運(yùn)行結(jié)果如圖4.4所示。
圖4.4 例4-4運(yùn)行結(jié)果
例4-4中,實(shí)例化對(duì)象時(shí),調(diào)用了有參構(gòu)造方法,在該方法中通過this()調(diào)用了無(wú)參構(gòu)造方法。因此,運(yùn)行結(jié)果中顯示兩個(gè)構(gòu)造方法都被調(diào)用了。
在使用this調(diào)用構(gòu)造方法時(shí),還需注意:在構(gòu)造方法中,使用 this調(diào)用構(gòu)造方法的語(yǔ)句必須位于首行,且只能出現(xiàn)一次,如例4-5所示。
例4-5 TestThisRefConstructor01.java
1 class Person {
2 private String name; // 姓名
3 private int age; // 年齡
4 public Person() {
5 System.out.println(“調(diào)用無(wú)參構(gòu)造方法”);
6 }
7 public Person(String name, int age) {
8 System.out.println(“調(diào)用有參構(gòu)造函數(shù)”);
9 this.name = name;
10 this.age = age;
11 this(); // 調(diào)用無(wú)參構(gòu)造函數(shù)
12 }
13 public void say() {
14 System.out.println(“姓名:”+this.name+",年齡:"+this.age);
15 }
16 }
程序的運(yùn)行結(jié)果如圖4.5所示。
圖4.5 例4-5運(yùn)行結(jié)果
在圖4.5中,編譯報(bào)錯(cuò)并提示“對(duì)this的調(diào)用必須是構(gòu)造器中的第一個(gè)語(yǔ)句”。因此在使用this()調(diào)用構(gòu)造方法必須位于構(gòu)造方法的第一行。
另外,this調(diào)用構(gòu)造方法時(shí),一定要留一個(gè)構(gòu)造方法作為出口,即至少存在一個(gè)構(gòu)造方法不使用this調(diào)用其他構(gòu)造方法,如例4-6所示。
例4-6 TestThisRefConstructor02.java
1 class Person {
2 private String name; // 姓名
3 private int age; // 年齡
4 public Person() {
5 this(null, 0); // 調(diào)用有參構(gòu)造函數(shù)
6 System.out.println(“調(diào)用無(wú)參構(gòu)造方法”);
7 }
8 public Person(String name, int age) {
9 this(); // 調(diào)用無(wú)參構(gòu)造函數(shù)
10 System.out.println(“調(diào)用有參構(gòu)造函數(shù)”);
11 this.name = name;
12 this.age = age;
13 }
14 public void say() {
15 System.out.println(“姓名:”+this.name+",年齡:"+this.age);
16 }
17 }
程序的運(yùn)行結(jié)果如圖4.6所示。
圖4.6 例4-6運(yùn)行結(jié)果
在圖4.6中,編譯報(bào)錯(cuò)并提示“遞歸構(gòu)造器調(diào)用”。因此,在構(gòu)造方法互相調(diào)用時(shí),一定要預(yù)留一個(gè)出口,一般將無(wú)參構(gòu)造方法作為出口,即在無(wú)參構(gòu)造方法中不再去調(diào)用其他構(gòu)造方法。
本文來(lái)自千鋒教育,轉(zhuǎn)載請(qǐng)注明出處。
總結(jié)
以上是生活随笔為你收集整理的Java培训教程之this关键字讲解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习Python必须具备的五大技能!
- 下一篇: Java培训教程之对象的创建与使用