Java super關(guān)鍵字
super 關(guān)鍵字與 this 類似,this 用來表示當(dāng)前類的實例,super 用來表示父類。
super 可以用在子類中,通過點號(.)來獲取父類的成員變量和方法。super 也可以用在子類的子類中,Java 能自動向上層類追溯。
父類行為被調(diào)用,就好象該行為是本類的行為一樣,而且調(diào)用行為不必發(fā)生在父類中,它能自動向上層類追溯。
super 關(guān)鍵字的功能:
調(diào)用父類中聲明為 private 的變量。
點取已經(jīng)覆蓋了的方法。
作為方法名表示父類構(gòu)造方法。
調(diào)用隱藏變量和被覆蓋的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class Demo{ public static void main(String[] args) { Dog obj = new Dog(); obj.move(); } } class Animal{ private String desc = "Animals are human's good friends" ; // 必須要聲明一個 getter 方法 public String getDesc() { return desc; } public void move(){ System.out.println( "Animals can move" ); } } class Dog extends Animal{ public void move(){ super .move(); // 調(diào)用父類的方法 System.out.println( "Dogs can walk and run" ); // 通過 getter 方法調(diào)用父類隱藏變量 System.out.println( "Please remember: " + super .getDesc()); } } |
運行結(jié)果:
1
2
3
|
Animals can move Dogs can walk and run Please remember: Animals are human's good friends |
move() 方法也可以定義在某些祖先類中,比如父類的父類,Java 具有追溯性,會一直向上找,直到找到該方法為止。
通過 super 調(diào)用父類的隱藏變量,必須要在父類中聲明 getter 方法,因為聲明為 private 的數(shù)據(jù)成員對子類是不可見的。
調(diào)用父類的構(gòu)造方法
在許多情況下,使用默認(rèn)構(gòu)造方法來對父類對象進(jìn)行初始化。當(dāng)然也可以使用 super 來顯示調(diào)用父類的構(gòu)造方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class Demo{ public static void main(String[] args) { Dog obj = new Dog( "花花" , 3 ); obj.say(); } } class Animal{ String name; public Animal(String name){ this .name = name; } } class Dog extends Animal{ int age; public Dog(String name, int age){ super (name); this .age = age; } public void say(){ System.out.println( "我是一只可愛的小狗,我的名字叫" + name + ",我" + age + "歲了" ); } } |
運行結(jié)果:
我是一只可愛的小狗,我的名字叫花花,我3歲了
注意:無論是 super() 還是 this(),都必須放在構(gòu)造方法的第一行。
值得注意的是:
在構(gòu)造方法中調(diào)用另一個構(gòu)造方法,調(diào)用動作必須置于最起始的位置。
不能在構(gòu)造方法以外的任何方法內(nèi)調(diào)用構(gòu)造方法。
在一個構(gòu)造方法內(nèi)只能調(diào)用一個構(gòu)造方法。
如果編寫一個構(gòu)造方法,既沒有調(diào)用 super() 也沒有調(diào)用 this(),編譯器會自動插入一個調(diào)用到父類構(gòu)造方法中,而且不帶參數(shù)。
最后注意 super 與 this 的區(qū)別:super 不是一個對象的引用,不能將 super 賦值給另一個對象變量,它只是一個指示編譯器調(diào)用父類方法的特殊關(guān)鍵字。
Java instanceof 運算符
多態(tài)性帶來了一個問題,就是如何判斷一個變量所實際引用的對象的類型 。 C++使用runtime-type information(RTTI),Java 使用 instanceof 操作符。
instanceof 運算符用來判斷一個變量所引用的對象的實際類型,注意是它引用的對象的類型,不是變量的類型。請看下面的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public final class Demo{ public static void main(String[] args) { // 引用 People 類的實例 People obj = new People(); if (obj instanceof Object){ System.out.println( "我是一個對象" ); } if (obj instanceof People){ System.out.println( "我是人類" ); } if (obj instanceof Teacher){ System.out.println( "我是一名教師" ); } if (obj instanceof President){ System.out.println( "我是校長" ); } System.out.println( "-----------" ); // 分界線 // 引用 Teacher 類的實例 obj = new Teacher(); if (obj instanceof Object){ System.out.println( "我是一個對象" ); } if (obj instanceof People){ System.out.println( "我是人類" ); } if (obj instanceof Teacher){ System.out.println( "我是一名教師" ); } if (obj instanceof President){ System.out.println( "我是校長" ); } } } class People{ } class Teacher extends People{ } class President extends Teacher{ } |
運行結(jié)果:
1
2
3
4
5
6
|
我是一個對象 我是人類 ----------- 我是一個對象 我是人類 我是一名教師 |
可以看出,如果變量引用的是當(dāng)前類或它的子類的實例,instanceof 返回 true,否則返回 false。