Java-this、super關鍵字(認真分析)

來源:互聯網
上載者:User

標籤:code   rgs   子類   this調用   關聯   構造   分析   fat   i++   

一、this關鍵字

  this關鍵字只能用於方法體內,當建立一個對象時候,JVM就會為這個對象分配一個資深的指標,就是this。因此this只能在非靜態方法中使用,靜態方法和靜態代碼塊中絕對不能使用this,this只和特定的對象關聯,不和類關聯,同一個類的不同對象有不同的this。

 

舉個栗子:

 

 1 package cn.zpoor.Test; 2 /** 3  * @author 薛定諤的貓 4  * 測試this*/ 5 public class Main { 6     private String name; 7     private int age; 8     private String sex; 9     private int i = 33;10     //一個參數的構造器11     public Main(int n) {12         age = n;//可以寫成this.age = age;13     }14     15     //帶有兩個參數的構造器16     public Main(int age,String name,String sex) {17         //成員變數和參數同名,成員變數被屏蔽,用this來訪問成員變數18         this.name = name;19         this.sex = sex;20     }21     22     //預設構造器23     public Main() {24         this(0, null, null);//this調用另一個構造方法,並且只能放在第一行。25     }26     27     public Main(String name) {28         this(0, name, "man");29     }30     31     32     private void info(Main m) {33         System.out.println("-------------");34         System.out.println(m.name);35         System.out.println(m.age);36         System.out.println(m.sex);37         this.f();//寫可以寫成this.f();38     }39     40     private void f() {41         //局部變數和成員變數相同,成員變數被屏蔽,用this來訪問成員變數42         int i;43         i = this.i++;44         System.out.println(i);45         System.out.println(this.i);46     }47     48     //返回當前執行個體的引用49     50     private Main getSelf() {51         return this;52     }53     54     55     public static void main(String[] args) {56         Main m1 = new Main();57         Main m2 = new Main("zpoor");58         m1.info(m1);59         m2.info(m2);60     }61 }62 63 /*-------------64 null65 066 null67 3368 3469 -------------70 zpoor71 072 man73 3374 3475 76 */

 

 

不要慌(冷靜分析):

  一、通過this調用另一個構造方法,this(參數列表),僅僅在建構函式,別的地方不能用,而且要放在構造方法中的第一行

  二、方法的參數或者方法中的局部變數和成員變數重名的情況下,成員變數被屏蔽,就要用this.成員變數來引用成員變數。如果沒有重名用不用都不為過。

  三、在方法中,引用該方法所屬類的當前對象時候,直接用this

總結:this是指向對象本身的一個指標。

 

二、super關鍵字

  super和this類似,是被屏蔽的成員變數或者成員方法,或者是引用被屏蔽的成員變數和成員方法,一般都是發生在繼承裡面,super用在子類裡面,用來訪問父類中屏蔽的成員

 

舉個栗子:

 1 package cn.zpoor.Test; 2 /** 3  * @author 薛定諤的貓 4  * 測試super*/ 5 public class Father { 6     public String Value = "Father"; 7     public String x = "Father中的成員變數"; 8      9     public Father() {10         System.out.println("Father的構造器被調用");11     }12     13     public Father(String Value) {14         this.Value = "Father帶參數的構造器運行了";15     }16     17     public void info() {18         System.out.println("Father的info方法被調用");19     }20     public static void main(String[] args) {21         new Son().test();22     }23 }24 25 class Son extends Father {26     public String Value = "Son";27     28     public Son() {29         super();//調用父類的構造方法,只能必須放在第一行30         System.out.println("Son的無參方法被調用");31     }32     33     public Son(String name) {34         super(name);35         System.out.println("Son帶參數的構造器被調用");36     }37     38     //重寫父類info方法39     public void info() {40         System.out.println("Son的info方法被調用");41     }42     43     public void test() {44         String Value = "老鐵,得勁不?";//局部變數覆蓋成員變數和超類成員變數45         System.out.println("-----1-----");46         System.out.println(Value);//輸出局部變數Value47         System.out.println(this.Value);//輸出子類成員變數48         System.out.println(super.Value);//輸出父類的成員變數49         50         System.out.println("------2-----");51         System.out.println(x);//輸出超類成員變數x,子類繼承來的52         System.out.println(super.x);//輸出曹磊成員變數x53         54         System.out.println("--------3------");55         info();//調用子類的info方法56         this.info();//調用子類的info方法57         super.info();//調用超類的info方法58     }59 }60 61 /*62 Father的構造器被調用63 Son的無參方法被調用64 -----1-----65 老鐵,得勁不?66 Son67 Father68 ------2-----69 Father中的成員變數70 Father中的成員變數71 --------3------72 Son的info方法被調用73 Son的info方法被調用74 Father的info方法被調用75 */

 

 

總結(拿煙的手微微顫抖):

  一、子類構造方法要調用父類的構造方法,super(參數列表),參數不是必須的,但是放在第一行是一定的。

  二、當子類的局部變數或者子類的局部變數和父類的成員變數重名,也就是子類變數覆蓋父類變數的時候,super.成員變數來引用父類的成員變數,如果沒有覆 蓋,就不需要了,但是最好這樣,這是一個好習慣。

  三、當子類的成員方法覆蓋了父類的成員方法,就是子類的方法定義和父類的方法相同(重寫,覆蓋),super.方法名(參數列表)來調用父類的成員變數。

 

 

不行了,煙燙手。

 

Java-this、super關鍵字(認真分析)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.