<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## **super基本概念:** super 關鍵字與this類似,this 用來表示當前類的實例,super 用來表示父類。 super 可以用在子類中,通過點號(.)來獲取父類的成員變量和方法。super 也可以用在子類的子類中,Java 能自動向上層類追溯。 父類行為被調用,就好象該行為是本類的行為一樣,而且調用行為不必發生在父類中,它能自動向上層類追溯。 <br> **super 關鍵字的功能:** * 調用父類中聲明為 private 的變量。 * 點取已經覆蓋了的方法。 * 作為方法名表示父類構造方法。 <br> ## **調用隱藏變量和被覆蓋的方法:** ``` 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()); } } ``` 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 + "歲了"); } } ``` **注意:無論是 super() 還是 this(),都必須放在構造方法的第一行。** **值得注意的是:** * 在構造方法中調用另一個構造方法,調用動作必須置于最起始的位置。 * 不能在構造方法以外的任何方法內調用構造方法。 * 在一個構造方法內只能調用一個構造方法。 如果編寫一個構造方法,既沒有調用 super() 也沒有調用 this(),編譯器會自動插入一個調用到父類構造方法中,而且不帶參數。? 最后注意 super 與 this 的區別:super 不是一個對象的引用,不能將 super 賦值給另一個對象變量,它只是一個指示編譯器調用父類方法的特殊關鍵字。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看