qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java中super的使用

            一、super調用超類構造函數
            super(parameter-list);
            parameter-list指定超類中構造函數所需的任何形參,super()必須是在子類構造函數中執行的第一個語句。超類定義的任何形式的構造函數都可以被super()調用,被執行的構造函數就是與實參相匹配的那一個。
            當存在多重繼承時,super總是引用最靠近的超類的構造函數。例如:C類繼承B類,B類繼承A類,則C類的構造函數中使用super,引用B類的構造函數。
            如果不使用super(),那么就會執行每個超類的默認(無形參)構造函數。
            例如:
          <span style="font-size:14px;">public class test {
          public static void main(String [] args){
          new Teacher("one", 62);
          new Teacher("two", 180);
          new Teacher("three", 65, 188);
          }
          }
          class Human {
          static private float weight;
          static private int height;
          Human(float weight){
          Human.weight = weight;
          System.out.println(Human.weight);
          }
          Human(int height){
          Human.height = height;
          System.out.println(Human.height);
          }
          Human(float weight, int height){
          Human.weight = weight;
          Human.height = height;
          System.out.println(Human.weight + " " + Human.height);
          }
          }
          class Teacher extends Human {
          static private String s;
          Teacher(String s, float weight){
          super(weight);
          Teacher.s = s;
          System.out.println(Teacher.s);
          }
          Teacher(String s, int height){
          super(height);
          Teacher.s = s;
          System.out.println(Teacher.s);
          }
          Teacher(String s, float weight, int height){
          super(weight, height);
          Teacher.s = s;
          System.out.println(Teacher.s);
          }
          }
          </span>
            結果:
            62
            one
            180
            two
            65.0 188
            three
           二、super訪問超類的成員
            例如:
          <span style="font-size:14px;">public class test {
          public static void main(String [] args){
          new Teacher("three", 65, 188);
          }
          }
          class Human {
          static public float weight;
          static public int height;
          }
          class Teacher extends Human {
          static private String s;
          Teacher(String s, float weight, int height){
          super.weight = weight;
          super.height = height;
          Teacher.s = s;
          System.out.println(super.weight);
          System.out.println(super.height);
          System.out.println(Teacher.s);
          }
          }</span>
            結果:
            65.0
            188
            three
            三、super和this的區別
            1)super(參數):調用基類中的某一個構造函數(應該為構造函數中的第一條語句);this(參數):調用本類中另一種形成的構造函數(應該為構造函數中的第一條語句);
            2)super引用當前對象的直接父類中的成員(用來訪問直接父類中被隱藏的父類中成員數據或函數,基類與派生類中有相同成員定義時如:super.變量名    super.成員函數據名(實參);this代表當前對象名(在程序中易產生二義性之處,應使用this來指明當前對象;如果函數的形參與類中的成員數據同名,這時需用this來指明成員變量名);
            3)調用super()必須寫在子類構造方法的第一行,否則編譯不通過。每個子類構造方法的第一條語句,都是隱含地調用super(),如果父類沒有這種形式的構造函數,那么在編譯的時候就會報錯;super()和this()類似,區別是,super()從子類中調用父類的構造方法,this()在同一類內調用其它方法;
            4)super()和this()均需放在構造方法內第一行;
            5)盡管可以用this調用一個構造器,但卻不能調用兩個;
            6)this和super不能同時出現在一個構造函數里面,因為this必然會調用其它的構造函數,其它的構造函數必然也會有super語句的存在,所以在同一個構造函數里面有相同的語句,就失去了語句的意義,編譯器也不會通過;
            7)this()和super()都指的是對象,所以,均不可以在static環境中使用。包括:static變量,static方法,static語句塊;
            8)從本質上講,this是一個指向本對象的指針, 然而super是一個Java關鍵字。

          posted on 2014-04-24 10:28 順其自然EVO 閱讀(216) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2014年4月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 彝良县| 玉门市| 开封市| 始兴县| 含山县| 彭阳县| 宿州市| 民乐县| 都安| 五莲县| 金阳县| 南华县| 江口县| 陆川县| 黄龙县| 丰顺县| 廉江市| 赣榆县| 台东县| 陵川县| 苏尼特左旗| 阜新市| 大名县| 贵阳市| 大余县| 文安县| 纳雍县| 哈尔滨市| 江华| 永定县| 那曲县| 丹阳市| 永顺县| 木里| 嘉善县| 青川县| 黄陵县| 留坝县| 巴彦淖尔市| 武穴市| 洛宁县|