這幾天看到類在繼承時會用到this和super,這裏就作了一點總結,與各位共同交流,有錯誤請各位指正~java
1、this函數
this是自身的一個對象,表明對象自己,能夠理解爲:指向對象自己的一個指針。this
this的用法在java中大致能夠分爲3種:spa
1.普通的直接引用設計
這種就不用講了,this至關因而指向當前對象自己。指針
2.形參與成員名字重名,用this來區分:code
package com.demo; public class Person { private int age = 10; public Person(){ System.out.println("初始化年齡:"+age); } public int GetAge(int age){ this.age = age; return this.age; } }
package com.demo; public class Test1 { public static void main(String[] args) { Person Harry = new Person(); System.out.println("Harry's age is "+Harry.GetAge(12)); } }
運行結果:對象
初始化年齡:10
Harry's age is 12
3.引用構造函數blog
這個和super放在一塊兒講,見下面。繼承
2、super
super能夠理解爲是指向本身超(父)類對象的一個指針,而這個超類指的是離本身最近的一個父類。
super也有三種用法:
1.普通的直接引用
與this相似,super至關因而指向當前對象的父類,這樣就能夠用super.xxx來引用父類的成員。
2.子類中的成員變量或方法與父類中的成員變量或方法同名
package com.demo; public class Country { String name; void value() { name = "China"; } }
package com.demo; public class City extends Country{ String name; void value() { name = "Shanghai"; super.value(); // 調用父類的方法 System.out.println(name); System.out.println(super.name); } public static void main(String[] args) { City c = new City(); c.value(); } }
運行結果:
Shanghai
China
能夠看到,這裏既調用了父類的方法,也調用了父類的變量。若不調用父類方法value(),只調用父類變量name的話,則父類name值爲默認值null。
3.引用構造函數
package com.demo.test; public class Person { public static void prt(String s) { System.out.println(s); } Person() { prt("父類·無參數構造方法: " + "A Person."); }// 構造方法(1) Person(String name) { prt("父類·含一個參數的構造方法: " + "A person's name is " + name); }// 構造方法(2) }
package com.demo.test; public class Chinese extends Person { Chinese() { super(); // 調用父類構造方法(1) prt("子類·調用父類「無參數構造方法「: " + "A chinese coder."); } Chinese(String name) { super(name);// 調用父類具備相同形參的構造方法(2) prt("子類·調用父類」含一個參數的構造方法「: " + "his name is " + name); } Chinese(String name, int age) { this(name);// 調用具備相同形參的構造方法(3) prt("子類:調用子類具備相同形參的構造方法:his age is " + age); } public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("codersai"); cn = new Chinese("codersai", 18); } }
運行結果:
父類·無參數構造方法: A Person. 子類·調用父類「無參數構造方法「: A chinese coder. 父類·含一個參數的構造方法: A person's name is codersai 子類·調用父類」含一個參數的構造方法「: his name is codersai 父類·含一個參數的構造方法: A person's name is codersai 子類·調用父類」含一個參數的構造方法「: his name is codersai 子類:調用子類具備相同形參的構造方法:his age is 18
從本例能夠看到,能夠用super和this分別調用父類的構造方法和本類中其餘形式的構造方法。
例子中Chinese類第三種構造方法調用的是本類中第二種構造方法,而第二種構造方法是調用父類的,所以也要先調用父類的構造方法,再調用本類中第二種,最後是重寫第三種構造方法。
3、super 和 this 的異同
1、this
Java關鍵字this只能用於方法體內。當一個對象建立後,Java虛擬機(JVM)就會給這個對象分配一個引用自身的指針,這個指針的名字就是this。所以,this只能在類中的非靜態方法中使用,靜態方法和靜態的代碼塊中絕對不能出現this。而且this只和特定的對象關聯,而不和類關聯,同一個類的不一樣對象有不一樣的this。下面給出一個使用this的綜合實例,以便說明問題:
package com.demo.test; public class Testthis { private int number; private String username; private String password; private int x = 100; public Testthis(int n) { number = n; // 這個還能夠寫爲: this.number=n; } public Testthis(int i, String username, String password) { // 成員變量和參數同名,成員變量被屏蔽,用"this.成員變量"的方式訪問成員變量. this.username = username; this.password = password; } // 默認不帶參數的構造方法 public Testthis() { this(0, "未知", "空"); // 經過this調用另外一個構造方法 } public Testthis(String name) { this(1, name, "空"); // 經過this調用另外一個構造方法 } public static void main(String args[]) { Testthis t1 = new Testthis(); Testthis t2 = new Testthis("遊客"); t1.outinfo(t1); t2.outinfo(t2); } private void outinfo(Testthis t) { System.out.println("-----------"); System.out.println(t.number); System.out.println(t.username); System.out.println(t.password); f(); // 這個能夠寫爲: this.f(); } private void f() { // 局部變量與成員變量同名,成員變量被屏蔽,用"this.成員變量"的方式訪問成員變量. int x; x = this.x++; System.out.println(x); System.out.println(this.x); } // 返回當前實例的引用 private Testthis getSelf() { return this; } }
運行結果以下:
----------- 0 未知 空 100 101 ----------- 0 遊客 空 100 101
看着上面的例子,說明在什麼狀況下須要用到this:
第1、經過this調用另外一個構造方法,用法是this(參數列表),這個僅僅在類的構造方法中,別的地方不能這麼用。
第2、函數參數或者函數中的局部變量和成員變量同名的狀況下,成員變量被屏蔽,此時要訪問成員變量則須要用「this.成員變量名」的方式來引用成員變量。固然,在沒有同名的狀況下,能夠直接用成員變量的名字,而不用this,用了也不爲錯,呵呵。
第3、在函數中,須要引用該函數所屬類的當前對象的時候,直接用this。其實這些用法總結都是從對「this是指向對象自己的一個指針」這句話的更深刻的理解而來的,死記否則容易忘記並且容易搞錯,要理解!
2、super
package com.demo.test; public class Father { public String v = "Father"; public String x = "輸出了Father類的public成員變量x!!!"; public Father() { System.out.println("Father構造方法被調用!"); } public Father(String v) { this.v = "Father類的帶參數構造方法!運行了."; } public void outinfo() { System.out.println("Father的outinfo方法被調用"); } public static void main(String[] args) { // TODO 自動生成方法存根 } }
package com.demo.test; public class Son extends Father { public String v = "Son"; public Son() { super(); // 調用超類的構造方法,只能放到第一行. System.out.println("Son無參數構造方法被調用!"); // super(); //錯誤的,必須放到構造方法體的最前面. } public Son(String str) { super(str); System.out.println("Son帶參數構造方法被調用!"); } // 覆蓋了超類成員方法outinfo() public void outinfo() { System.out.println("Son的outinfo()方法被調用"); } public void test() { String v = "哈哈哈哈!"; // 局部變量v覆蓋了成員變量v和超類變量v System.out.println("------1-----"); System.out.println(v); // 輸出局部變量v System.out.println(this.v); // 輸出(子類)成員變量v System.out.println(super.v); // 輸出超類成員變量v System.out.println("------2-----"); System.out.println(x); // 輸出超類成員變量v,子類繼承而來 System.out.println(super.x); // 輸出超類成員變量v System.out.println("------3-----"); outinfo(); // 調用子類的outinfo()方法 this.outinfo(); // 調用子類的outinfo()方法 super.outinfo(); // 調用父類的outinfo()方法 } public static void main(String[] args) { new Son().test(); } }
運行結果:
Father構造方法被調用! Son無參數構造方法被調用! ------1----- 哈哈哈哈! Son Father ------2----- 輸出了Father類的public成員變量x!!! 輸出了Father類的public成員變量x!!! ------3----- Son的outinfo()方法被調用 Son的outinfo()方法被調用 Father的outinfo方法被調用
說明:此例子僅僅爲了說明super的用法,實際在設計類的時候通常都儘量私有(private)化。
經過上面的例子,下面總結一下super的用法:
第1、在子類構造方法中要調用父類的構造方法,用「super(參數列表)」的方式調用,參數不是必須的。同時還要注意的一點是:「super(參數列表)」這條語句只能用在子類構造方法體中的第一行。
第2、當子類方法中的局部變量或者子類的成員變量與父類成員變量同名時,也就是子類局部變量覆蓋父類成員變量時,用「super.成員變量名」來引用父類成員變量。固然,若是父類的成員變量沒有被覆蓋,也能夠用「super.成員變量名」來引用父類成員變量,不過這是沒必要要的。
第3、當子類的成員方法覆蓋了父類的成員方法時,也就是子類和父類有徹底相同的方法定義(但方法體能夠不一樣),此時,用「super.方法名(參數列表)」的方式訪問父類的方法。
this、super的用法也不過這些,只有理解了其中的原理,纔不會跌入陷阱!