我的漫漫程序之旅

          專注于JavaWeb開(kāi)發(fā)
          隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

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

          繼承時(shí)候類的執(zhí)行順序問(wèn)題,一般都是選擇題,問(wèn)你將會(huì)打印出什么?
          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);
              }

              
          }


          輸出結(jié)果:
          parent static code
          child 
          static
           code
          ***************

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

          由此可看出在還沒(méi)有實(shí)例化類的時(shí)候(注意*號(hào))已經(jīng)執(zhí)行了static代碼塊。
          順序是先父類后子類.
          然后才調(diào)用父類的構(gòu)造方法,再調(diào)用子類的構(gòu)造方法.就是這個(gè)順序了.
          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 々上善若水々 閱讀(2748) 評(píng)論(7)  編輯  收藏 所屬分類: Java筆試與面試

          評(píng)論

          # re: 2007最后一套JAVA面試題(繼承)[未登錄](méi)  回復(fù)  更多評(píng)論   

          順序好像是這個(gè)樣子的啊
          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面試題(繼承)[未登錄](méi)  回復(fù)  更多評(píng)論   

          child并不是內(nèi)部類
          2008-02-15 14:15 | 1

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

          @1
          請(qǐng)動(dòng)手后再回復(fù).
          2008-02-17 21:49 | 々上善若水々

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

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

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

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

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

          我也動(dòng)手試過(guò)了

          無(wú)論把Parent、Child分開(kāi)放在兩個(gè)java文件中,還是放在一個(gè)java文件中,執(zhí)行后的結(jié)果都和樓上的一樣。

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

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

          對(duì)。。程序從main90方法入口。后面的子類都沒(méi)有實(shí)例化調(diào)用。。
          2008-08-16 21:20 | shady
          主站蜘蛛池模板: 武冈市| 五河县| 满洲里市| 屯昌县| 华坪县| 正阳县| 原平市| 长海县| 宜兰县| 桂阳县| 中西区| 罗定市| 谢通门县| 赣榆县| 合山市| 湘西| 探索| 广平县| 东乌珠穆沁旗| 行唐县| 镇赉县| 巴彦淖尔市| 益阳市| 湘潭市| 新密市| 安丘市| 敖汉旗| 维西| 宁夏| 赤壁市| 三门县| 滦平县| 广水市| 大田县| 夏河县| 六安市| 西华县| 宁河县| 泰顺县| 甘孜县| 汪清县|