我的漫漫程序之旅

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

          導航

          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

          常用鏈接

          留言簿(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 々上善若水々 閱讀(2752) 評論(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
          主站蜘蛛池模板: 沙坪坝区| 瓦房店市| 清河县| 鄂伦春自治旗| 珠海市| 新泰市| 吕梁市| 林州市| 滦平县| 怀远县| 永修县| 封开县| 梁山县| 兰西县| 大冶市| 浦江县| 康平县| 大荔县| 工布江达县| 仙游县| 怀安县| 西昌市| 凤城市| 北辰区| 桂东县| 三明市| 庆城县| 安义县| 鲜城| 化德县| 寻甸| 古蔺县| 辽阳县| 普格县| 沈阳市| 鸡西市| 莱州市| 钟山县| 黄大仙区| 石家庄市| 通化县|