Java的super關鍵字與instanceof運算子使用方法_java

來源:互聯網
上載者:User

Java super關鍵字
super 關鍵字與 this 類似,this 用來表示當前類的執行個體,super 用來表示父類。

super 可以用在子類中,通過點號(.)來擷取父類的成員變數和方法。super 也可以用在子類的子類中,Java 能自動向上層類追溯。

父類行為被調用,就好象該行為是本類的行為一樣,而且調用行為不必發生在父類中,它能自動向上層類追溯。

super 關鍵字的功能:
調用父類中聲明為 private 的變數。
點取已經覆蓋了的方法。
作為方法名表示父類構造方法。
調用隱藏變數和被覆蓋的方法

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(); // 調用父類的方法    System.out.println("Dogs can walk and run");    // 通過 getter 方法調用父類隱藏變數    System.out.println("Please remember: " + super.getDesc());  }}

運行結果:

Animals can moveDogs can walk and runPlease remember: Animals are human's good friends

move() 方法也可以定義在某些祖先類中,比如父類的父類,Java 具有追溯性,會一直向上找,直到找到該方法為止。

通過 super 調用父類的隱藏變數,必須要在父類中聲明 getter 方法,因為聲明為 private 的資料成員對子類是不可見的。
調用父類的構造方法

在許多情況下,使用預設構造方法來對父類對象進行初始化。當然也可以使用 super 來顯示調用父類的構造方法。

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 + "歲了");  }}

運行結果:
我是一隻可愛的小狗,我的名字叫花花,我3歲了

注意:無論是 super() 還是 this(),都必須放在構造方法的第一行。

值得注意的是:
在構造方法中調用另一個構造方法,調用動作必須置於最起始的位置。
不能在構造方法以外的任何方法內調用構造方法。
在一個構造方法內只能調用一個構造方法。

如果編寫一個構造方法,既沒有調用 super() 也沒有調用 this(),編譯器會自動插入一個調用到父類構造方法中,而且不帶參數。

最後注意 super 與 this 的區別:super 不是一個對象的引用,不能將 super 賦值給另一個物件變數,它只是一個指示編譯器調用父類方法的特殊關鍵字。

Java instanceof 運算子
多態性帶來了一個問題,就是如何判斷一個變數所實際引用的對象的類型 。 C++使用runtime-type information(RTTI),Java 使用 instanceof 操作符。

instanceof 運算子用來判斷一個變數所引用的對象的實際類型,注意是它引用的對象的類型,不是變數的類型。請看下面的代碼:

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{ }

運行結果:

我是一個對象我是人類-----------我是一個對象我是人類我是一名教師

可以看出,如果變數引用的是當前類或它的子類的執行個體,instanceof 返回 true,否則返回 false。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.