成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

我妹說,只用講 This,不用講 Super

開發 前端
“哥,被喊大舅子的感覺怎么樣啊?”三妹不懷好意地對我說,她眼睛里充滿著不屑。

[[357937]]

 “哥,被喊大舅子的感覺怎么樣啊?”三妹不懷好意地對我說,她眼睛里充滿著不屑。

“說實話,這種感覺還不錯。”我有點難為情的回答她,“不過,有一點令我感到些許失落。大家的焦點似乎都是你的顏值,完全忽略了我的盛世美顏啊!”

“哥,你想啥呢,那是因為你文章寫得好,不然誰認識我是誰啊!有你這樣的哥哥,我還是挺自豪的。”三妹鄭重其事地說,“話說今天咱學啥呢?”

“三妹啊,你這句話說得我喜歡。今天來學習一下 Java 中的 this 關鍵字吧。”喝了一口農夫山泉后,我對三妹說。

“this 關鍵字有很多種用法,其中最常用的一個是,它可以作為引用變量,指向當前對象。”我面帶著樸實無華的微笑繼續說,“除此之外, this 關鍵字還可以完成以下工作。”

  • 調用當前類的方法;
  • this() 可以調用當前類的構造方法;
  • this 可以作為參數在方法中傳遞;
  • this 可以作為參數在構造方法中傳遞;
  • this 可以作為方法的返回值,返回當前類的對象。

01、 指向當前對象

“三妹,來看下面這段代碼。”話音剛落,我就在鍵盤上噼里啪啦一陣敲。

  1. public class WithoutThisStudent { 
  2.     String name
  3.     int age; 
  4.  
  5.     WithoutThisStudent(String nameint age) { 
  6.         name = name
  7.         age = age; 
  8.     } 
  9.  
  10.     void out() { 
  11.         System.out.println(name+" " + age); 
  12.     } 
  13.  
  14.     public static void main(String[] args) { 
  15.         WithoutThisStudent s1 = new WithoutThisStudent("沉默王二", 18); 
  16.         WithoutThisStudent s2 = new WithoutThisStudent("沉默王三", 16); 
  17.  
  18.         s1.out(); 
  19.         s2.out(); 
  20.     } 

“在上面的例子中,構造方法的參數名和實例變量名相同,由于沒有使用 this 關鍵字,所以無法為實例變量賦值。”我抬起右手的食指,指著屏幕上的 name 和 age 對著三妹說。

“來看一下程序的輸出結果。”

  1. null 0 
  2.  
  3. null 0 

“從結果中可以看得出來,盡管創建對象的時候傳遞了參數,但實例變量并沒有賦值。這是因為如果構造方法中沒有使用 this 關鍵字的話,name 和 age 指向的并不是實例變量而是參數本身。”我把脖子扭向右側,看著三妹說。

“那怎么解決這個問題呢?哥。”三妹著急地問。

“如果參數名和實例變量名產生了沖突.....”我正準備給出答案,三妹打斷了我。

“難道用 this 嗎?”三妹脫口而出。

“哇,越來越棒了呀,你。”我感覺三妹在學習 Java 這條道路上逐漸有了自己主動思考的意愿。

“是的,來看加上 this 關鍵字后的代碼。”

安靜的屋子里又響起了一陣噼里啪啦的鍵盤聲。

  1. public class WithThisStudent { 
  2.     String name
  3.     int age; 
  4.  
  5.     WithThisStudent(String nameint age) { 
  6.         this.name = name
  7.         this.age = age; 
  8.     } 
  9.  
  10.     void out() { 
  11.         System.out.println(name+" " + age); 
  12.     } 
  13.  
  14.     public static void main(String[] args) { 
  15.         WithThisStudent s1 = new WithThisStudent("沉默王二", 18); 
  16.         WithThisStudent s2 = new WithThisStudent("沉默王三", 16); 
  17.  
  18.         s1.out(); 
  19.         s2.out(); 
  20.     } 

“再來看一下程序的輸出結果。”

  1. 沉默王二 18 
  2. 沉默王三 16 

“這次,實例變量有值了,在構造方法中,this.xxx 指向的就是實例變量,而不再是參數本身了。”我慢吞吞地說著,“當然了,如果參數名和實例變量名不同的話,就不必使用 this 關鍵字,但我建議使用 this 關鍵字,這樣的代碼更有意義。”

03、調用當前類的方法

“仔細聽,三妹,看我敲鍵盤的速度是不是夠快。”

  1. public class InvokeCurrentClassMethod { 
  2.     void method1() {} 
  3.     void method2() { 
  4.         method1(); 
  5.     } 
  6.  
  7.     public static void main(String[] args) { 
  8.         new InvokeCurrentClassMethod().method1(); 
  9.     } 

“仔細瞧,三妹,上面這段代碼中沒有見到 this 關鍵字吧?”我面帶著神秘的微笑,準備給三妹變個魔術。

“確實沒有,哥,我確認過了。”

“那接下來,神奇的事情就要發生了。”我突然感覺劉謙附身了。

我快速的在 classes 目錄下找到 InvokeCurrentClassMethod.class 文件,然后雙擊打開(IDEA 默認會使用 FernFlower 打開字節碼文件)。

  1. public class InvokeCurrentClassMethod { 
  2.     public InvokeCurrentClassMethod() { 
  3.     } 
  4.  
  5.     void method1() { 
  6.     } 
  7.  
  8.     void method2() { 
  9.         this.method1(); 
  10.     } 
  11.  
  12.     public static void main(String[] args) { 
  13.         (new InvokeCurrentClassMethod()).method1(); 
  14.     } 

“瞪大眼睛仔細瞧,三妹,this 關鍵字是不是出現了?”

“哇,真的呢,好神奇啊!”三妹為了配合我的演出,也是十二分的賣力。

“我們可以在一個類中使用 this 關鍵字來調用另外一個方法,如果沒有使用的話,編譯器會自動幫我們加上。”我對自己深厚的編程功底充滿自信,“在源代碼中,method2() 在調用 method1() 的時候并沒有使用 this 關鍵字,但通過反編譯后的字節碼可以看得到。”

04、調用當前類的構造方法

“再來看下面這段代碼。”

  1. public class InvokeConstrutor { 
  2.     InvokeConstrutor() { 
  3.         System.out.println("hello"); 
  4.     } 
  5.  
  6.     InvokeConstrutor(int count) { 
  7.         this(); 
  8.         System.out.println(count); 
  9.     } 
  10.  
  11.     public static void main(String[] args) { 
  12.         InvokeConstrutor invokeConstrutor = new InvokeConstrutor(10); 
  13.     } 

“在有參構造方法 InvokeConstrutor(int count) 中,使用了 this() 來調用無參構造方法 InvokeConstrutor()。”這次,我換成了左手的食指,指著屏幕對三妹說,“this() 可用于調用當前類的構造方法——構造方法可以重用了。”

“來看一下輸出結果。”

  1. hello 
  2. 10 

“真的啊,無參構造方法也被調用了,所以程序輸出了 hello。”三妹看到輸出結果后不假思索地說。

“也可以在無參構造方法中使用 this() 并傳遞參數來調用有參構造方法。”話音沒落,我就在鍵盤上敲了起來,“來看下面這段代碼。”

  1. public class InvokeParamConstrutor { 
  2.     InvokeParamConstrutor() { 
  3.         this(10); 
  4.         System.out.println("hello"); 
  5.     } 
  6.  
  7.     InvokeParamConstrutor(int count) { 
  8.         System.out.println(count); 
  9.     } 
  10.  
  11.     public static void main(String[] args) { 
  12.         InvokeParamConstrutor invokeConstrutor = new InvokeParamConstrutor(); 
  13.     } 

“再來看一下程序的輸出結果。”

  1. 10 
  2. hello 

“不過,需要注意的是,this() 必須放在構造方法的第一行,否則就報錯了。”

05、作為參數在方法中傳遞

“來看下面這段代碼。”

  1. public class ThisAsParam { 
  2.     void method1(ThisAsParam p) { 
  3.         System.out.println(p); 
  4.     } 
  5.  
  6.     void method2() { 
  7.         method1(this); 
  8.     } 
  9.  
  10.     public static void main(String[] args) { 
  11.         ThisAsParam thisAsParam = new ThisAsParam(); 
  12.         System.out.println(thisAsParam); 
  13.         thisAsParam.method2(); 
  14.     } 

“this 關鍵字可以作為參數在方法中傳遞,此時,它指向的是當前類的對象。”一不小心,半個小時過去了,我感到嗓子冒煙,于是趕緊又喝了一口水,潤潤嗓子后繼續說道。

“來看一下輸出結果,你就明白了,三妹。”

  1. com.itwanger.twentyseven.ThisAsParam@77459877 
  2. com.itwanger.twentyseven.ThisAsParam@77459877 

“method2() 調用了 method1(),并傳遞了參數 this,method1() 中打印了當前對象的字符串。main() 方法中打印了 thisAsParam 對象的字符串。從輸出結果中可以看得出來,兩者是同一個對象。”

06、作為參數在構造方法中傳遞

“繼續來看代碼。”

  1. public class ThisAsConstrutorParam { 
  2.     int count = 10; 
  3.  
  4.     ThisAsConstrutorParam() { 
  5.         Data data = new Data(this); 
  6.         data.out(); 
  7.     } 
  8.  
  9.     public static void main(String[] args) { 
  10.         new ThisAsConstrutorParam(); 
  11.     } 
  12.  
  13. class Data { 
  14.     ThisAsConstrutorParam param; 
  15.     Data(ThisAsConstrutorParam param) { 
  16.         this.param = param; 
  17.     } 
  18.  
  19.     void out() { 
  20.         System.out.println(param.count); 
  21.     } 

“在構造方法 ThisAsConstrutorParam() 中,我們使用 this 關鍵字作為參數傳遞給了 Data 對象,它其實指向的就是 new ThisAsConstrutorParam() 這個對象。”

“this 關鍵字也可以作為參數在構造方法中傳遞,它指向的是當前類的對象。當我們需要在多個類中使用一個對象的時候,這非常有用。”

“來看一下輸出結果。”

  1. 10 

07、作為方法的返回值

“需要休息會嗎?三妹”

“沒事的,哥,我的注意力還是很集中的,你繼續講吧。”

“好的,那來繼續看代碼。”

  1. public class ThisAsMethodResult { 
  2.     ThisAsMethodResult getThisAsMethodResult() { 
  3.         return this; 
  4.     } 
  5.      
  6.     void out() { 
  7.         System.out.println("hello"); 
  8.     } 
  9.  
  10.     public static void main(String[] args) { 
  11.         new ThisAsMethodResult().getThisAsMethodResult().out(); 
  12.     } 

“getThisAsMethodResult() 方法返回了 this 關鍵字,指向的就是 new ThisAsMethodResult() 這個對象,所以可以緊接著調用 out() 方法——達到了鏈式調用的目的,這也是 this 關鍵字非常經典的一種用法。”

“鏈式調用的形式在 JavaScript 代碼更加常見。”為了向三妹證實這一點,我打開了 jQuery 的源碼。

“原來這么多鏈式調用啊!”三妹感嘆到。

“是的。”我點點頭,然后指著 getThisAsMethodResult() 方法的返回值對三妹說,“需要注意的是,this 關鍵字作為方法的返回值的時候,方法的返回類型為類的類型。”

“來看一下輸出結果。”

  1. hello 

“那么,關于 this 關鍵字的介紹,就到此為止了。”我活動了一下僵硬的脖子后,對三妹說,“如果你學習勁頭還可以的話,我們順帶把 super 關鍵字捎帶著過一下,怎么樣?”

“不用了吧,聽說 super 關鍵字更簡單,我自己看看就行了,不用你講了!”

“不不不,三妹啊,你得假裝聽一下,不然我怎么向讀者們交差。”

“噢噢噢噢。”三妹意味深長地笑了。

08、super 關鍵字

“super 關鍵字的用法主要有三種。”

  • 指向父類對象;
  • 調用父類的方法;
  • super() 可以調用父類的構造方法。

“其實和 this 有些相似,只不過用意不大相同。”我端起水瓶,咕咚咕咚又喝了幾大口,好渴。“每當創建一個子類對象的時候,也會隱式的創建父類對象,由 super 關鍵字引用。”

“如果父類和子類擁有同樣名稱的字段,super 關鍵字可以用來訪問父類的同名字段。”

“來看下面這段代碼。”

  1. public class ReferParentField { 
  2.     public static void main(String[] args) { 
  3.         new Dog().printColor(); 
  4.     } 
  5.  
  6. class Animal { 
  7.     String color = "白色"
  8.  
  9. class Dog extends Animal { 
  10.     String color = "黑色"
  11.  
  12.     void printColor() { 
  13.         System.out.println(color); 
  14.         System.out.println(super.color); 
  15.     } 

“父類 Animal 中有一個名為 color 的字段,子類 Dog 中也有一個名為 color 的字段,子類的 printColor() 方法中,通過 super 關鍵字可以訪問父類的 color。”

“來看一下輸出結果。”

  1. 黑色 
  2. 白色 

“當子類和父類的方法名相同時,可以使用 super 關鍵字來調用父類的方法。換句話說,super 關鍵字可以用于方法重寫時訪問到父類的方法。”

  1. public class ReferParentMethod { 
  2.     public static void main(String[] args) { 
  3.         new Dog().work(); 
  4.     } 
  5.  
  6. class Animal { 
  7.     void eat() { 
  8.         System.out.println("吃..."); 
  9.     } 
  10.  
  11. class Dog extends Animal { 
  12.     @Override 
  13.     void eat() { 
  14.         System.out.println("吃..."); 
  15.     } 
  16.  
  17.     void bark() { 
  18.         System.out.println("汪汪汪..."); 
  19.     } 
  20.  
  21.     void work() { 
  22.         super.eat(); 
  23.         bark(); 
  24.     } 
  25. }   

“瞧,三妹。父類 Animal 和子類 Dog 中都有一個名為 eat() 的方法,通過 super.eat() 可以訪問到父類的 eat() 方法。”

等三妹在自我消化的時候,我在鍵盤上又敲完了一串代碼。

  1. public class ReferParentConstructor { 
  2.     public static void main(String[] args) { 
  3.         new Dog(); 
  4.     } 
  5.  
  6. class Animal { 
  7.     Animal(){ 
  8.         System.out.println("動物來了"); 
  9.     } 
  10.  
  11. class Dog extends Animal { 
  12.     Dog() { 
  13.         super(); 
  14.         System.out.println("狗狗來了"); 
  15.     } 

“子類 Dog 的構造方法中,第一行代碼為 super(),它就是用來調用父類的構造方法的。”

“來看一下輸出結果。”

  1. 動物來了 
  2. 狗狗來了 

“當然了,在默認情況下,super() 是可以省略的,編譯器會主動去調用父類的構造方法。也就是說,子類即使不使用 super() 主動調用父類的構造方法,父類的構造方法仍然會先執行。”

  1. public class ReferParentConstructor { 
  2.     public static void main(String[] args) { 
  3.         new Dog(); 
  4.     } 
  5.  
  6. class Animal { 
  7.     Animal(){ 
  8.         System.out.println("動物來了"); 
  9.     } 
  10.  
  11. class Dog extends Animal { 
  12.     Dog() { 
  13.         System.out.println("狗狗來了"); 
  14.     } 

“輸出結果和之前一樣。”

  1. 動物來了 
  2. 狗狗來了 

“super() 也可以用來調用父類的有參構造方法,這樣可以提高代碼的可重用性。”

  1. class Person { 
  2.     int id; 
  3.     String name
  4.  
  5.     Person(int id, String name) { 
  6.         this.id = id; 
  7.         this.name = name
  8.     } 
  9.  
  10. class Emp extends Person { 
  11.     float salary; 
  12.  
  13.     Emp(int id, String namefloat salary) { 
  14.         super(id, name); 
  15.         this.salary = salary; 
  16.     } 
  17.  
  18.     void display() { 
  19.         System.out.println(id + " " + name + " " + salary); 
  20.     } 
  21.  
  22. public class CallParentParamConstrutor { 
  23.     public static void main(String[] args) { 
  24.         new Emp(1, "沉默王二", 20000f).display(); 
  25.     } 

“Emp 類繼承了 Person 類,也就繼承了 id 和 name 字段,當在 Emp 中新增了 salary 字段后,構造方法中就可以使用 super(id, name) 來調用父類的有參構造方法。”

“來看一下輸出結果。”

  1. 1 沉默王二 20000.0 

三妹點了點頭,所有所思。

09、ending

“三妹,this 和 super 關鍵字我們就學到這里吧,你還有什么問題嗎?”三妹學習 Java 的勁頭讓我對她未來的編程生涯充滿了信心。

“沒有了,哥,你講的挺棒的,我已經全部都消化了。”三妹的臉上帶著微笑,“對了,哥,《教妹學 Java》已經更新到第 20 講了,你的 PDF 別忘記同步更新啊!”

“一定一定。”

聽我說完,三妹放心地回她自己的小屋休息去了。我趁她不在的這一會時間,把這篇文章編輯到了“沉默王二”公眾號,滿懷期許地等待留言區的新一波“大舅哥”。

本文轉載自微信公眾號「沉默王二」,可以通過以下二維碼關注。轉載本文請聯系沉默王二公眾號。 

 

責任編輯:武曉燕 來源: 沉默王二
相關推薦

2021-03-04 08:06:15

ZooKeeper集群代碼

2017-05-19 15:17:55

Android模塊化代碼

2012-12-26 16:10:25

蘋果AndroidiOS

2010-07-28 15:10:21

NFS配置

2020-09-25 16:40:52

Selenium

2020-12-10 08:43:17

垃圾回收JVM

2019-07-03 15:14:00

Oracle存儲結構

2010-07-21 16:57:44

telnet命令

2022-05-24 08:09:00

HadoopHiveSpark

2022-11-02 15:35:35

Condition代碼線程

2021-11-26 00:00:20

Consumer 接口代碼

2010-01-25 15:15:46

Android傳值

2010-01-14 09:27:44

C++語言

2017-08-16 09:55:36

2010-09-26 11:17:55

dhcp relay配

2022-01-17 07:59:13

SpringSpringMVCSpringBoot

2010-02-22 11:02:55

Python功能

2010-03-29 22:33:39

2012-05-04 13:36:09

HTML5

2019-04-08 12:14:59

Elasticsear程序員Lucene
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产午夜精品久久久 | 亚洲成人一区二区三区 | 欧美一区二区三区在线播放 | 国产精品永久免费观看 | 男女爱爱福利视频 | 亚洲激情网站 | 国产精品高潮呻吟久久av黑人 | 欧美日韩精品在线免费观看 | 日韩一区二区在线观看视频 | 国产精品成人一区二区 | 国产精品久久久久久模特 | 精品一区二区视频 | 成人在线一区二区 | 羞羞网站在线观看 | 丁香婷婷久久久综合精品国产 | 欧美成人a| 亚洲一视频 | 懂色av色香蕉一区二区蜜桃 | 97国产精品视频人人做人人爱 | 亚洲区中文字幕 | 精品乱码一区二区 | 999免费观看视频 | 狠狠色网 | 91视视频在线观看入口直接观看 | 精品国产31久久久久久 | 日韩在线不卡视频 | 国产精品视频999 | 在线视频亚洲 | 欧洲精品在线观看 | 精品少妇一区二区三区在线播放 | 午夜精品福利视频 | 在线观看av网站 | 亚洲精品 在线播放 | 一区二区三区免费观看 | 亚洲草草视频 | 国产精品黄视频 | 中文字幕在线第一页 | 美女中文字幕视频 | 色就干 | 综合久久综合久久 | 97精品国产手机 |