我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          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
          主站蜘蛛池模板: 阿拉善左旗| 六盘水市| 永兴县| 连平县| 绍兴县| 景德镇市| 沁水县| 卫辉市| 米林县| 临城县| 石楼县| 吴桥县| 迁西县| 板桥市| 班戈县| 伽师县| 黔西县| 黑水县| 叙永县| 新蔡县| 朝阳市| 金沙县| 茶陵县| 怀安县| 阳高县| 五家渠市| 康乐县| 焦作市| 仁寿县| 浙江省| 河源市| 阿克陶县| 滨州市| 阳朔县| 阳泉市| 岢岚县| 上栗县| 诸城市| 北海市| 色达县| 中江县|