我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0

          導航

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(39)

          隨筆檔案(43)

          文章分類(304)

          文章檔案(257)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          2007最后一套JAVA面試題(繼承)

          繼承時候類的執行順序問題,一般都是選擇題,問你將會打印出什么?
          package com.test;

          public class
           Parent
          {
              
          //1

              static int a =  1;
              
          //2

              static
              
          {
                  a 
          = 10
          ;
                  System.out.println(
          "parent static code"
          );
              }

              
          //4
              public Parent()
              
          {
                  System.out.println(
          "Parent constructor"
          );
                  System.out.println(
          "Parent a=" +
           a);
              }

              
              
          public static void main(String[] args)
              
          {
                  System.out.println(
          "***************"
          );
                  Parent c 
          = new
           Child();
              }

          }


          class Child extends Parent
          {
              
          static int a = 2
          ;
              
          //3

              static 
              
          {
                  a 
          = 20
          ;
                  System.out.println(
          "child static code"
          );
              }

              
          //5
              public Child()
              
          {
                  System.out.println(
          "Child constructor"
          );
                  System.out.println(
          "Child var a=" +
           a);
              }

              
          }


          輸出結果:
          parent static code
          child 
          static
           code
          ***************

          Parent constructor
          Parent a
          =10
          Child constructor
          Child var a
          =20

          由此可看出在還沒有實例化類的時候(注意*號)已經執行了static代碼塊。
          順序是先父類后子類.
          然后才調用父類的構造方法,再調用子類的構造方法.就是這個順序了.
          package com.test;

          public class
           Parent
          {
              
          //1

              static int a =  1;
              
          //2

              static
              
          {
                  a 
          = 10
          ;
                  System.out.println(
          "parent static code"
          );
              }

              
          //4
              public Parent()
              
          {
                  System.out.println(
          "Parent constructor"
          );
                  System.out.println(
          "Parent a=" +
           a);
              }

              
              
          public static void main(String[] args)
              
          {
                  System.out.println(
          "***************"
          );
                  Parent c 
          = new
           Child();
              }

          }


          class Child extends Parent
          {
              
          static int a = 2
          ;
              
          //3

              static 
              
          {
                  a 
          = 20
          ;
                  System.out.println(
          "child static code"
          );
              }

              
          //5
              public Child()
              
          {
                  System.out.println(
          "Child constructor"
          );
                  System.out.println(
          "Child var a=" +
           a);
              }

              
          }



          posted on 2007-12-28 10:36 々上善若水々 閱讀(2746) 評論(7)  編輯  收藏 所屬分類: Java筆試與面試

          評論

          # re: 2007最后一套JAVA面試題(繼承)[未登錄]  回復  更多評論   

          順序好像是這個樣子的啊
          parent static code
          ***************
          child static code
          Parent constructor
          Parent a=10
          Child constructor
          Child var a=20
          2008-02-15 14:09 | 1

          # re: 2007最后一套JAVA面試題(繼承)[未登錄]  回復  更多評論   

          child并不是內部類
          2008-02-15 14:15 | 1

          # re: 2007最后一套JAVA面試題(繼承)  回復  更多評論   

          @1
          請動手后再回復.
          2008-02-17 21:49 | 々上善若水々

          # re: 2007最后一套JAVA面試題(繼承)  回復  更多評論   

          @1
          論主的答案是正確的,要求把兩個類放在一個java文件里才行
          2008-03-12 20:54 | 天才

          # re: 2007最后一套JAVA面試題(繼承)  回復  更多評論   

          parent static code
          ***************
          child static code
          Parent constructor
          Parent a=10
          Child constructor
          Child var a=20
          肯定是這樣的,我運行過了。
          2008-04-20 11:41 | nene

          # re: 2007最后一套JAVA面試題(繼承)  回復  更多評論   

          我也動手試過了

          無論把Parent、Child分開放在兩個java文件中,還是放在一個java文件中,執行后的結果都和樓上的一樣。

          ps: 我的 java 版本是1.6
          2008-05-15 22:50 | gvn

          # re: 2007最后一套JAVA面試題(繼承)  回復  更多評論   

          對。。程序從main90方法入口。后面的子類都沒有實例化調用。。
          2008-08-16 21:20 | shady
          主站蜘蛛池模板: 南漳县| 遂昌县| 浏阳市| 三穗县| 兴海县| 西乌珠穆沁旗| 霍林郭勒市| 茂名市| 新营市| 玛曲县| 霸州市| 韶关市| 雅江县| 久治县| 房产| 利川市| 德昌县| 隆回县| 东兴市| 永修县| 彰武县| 于田县| 乐山市| 海宁市| 博乐市| 阆中市| 辉南县| 桂林市| 勃利县| 富源县| 富裕县| 宁乡县| 丰原市| 固始县| 瓦房店市| 西充县| 靖边县| 柯坪县| 甘孜| 山阳县| 余庆县|