<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

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

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

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

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

    評論

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

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

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

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

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

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

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

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

    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面試題(繼承)  回復(fù)  更多評論   

    我也動手試過了

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

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

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

    對。。程序從main90方法入口。后面的子類都沒有實例化調(diào)用。。
    2008-08-16 21:20 | shady
    主站蜘蛛池模板: 亚洲AV永久无码精品一百度影院| 97国产免费全部免费观看| 日韩成人在线免费视频 | 美国免费高清一级毛片| 国内一级一级毛片a免费| 亚洲精华国产精华精华液网站| 日本免费v片一二三区| 6080午夜一级毛片免费看 | 日本高清在线免费| 黄页网站在线免费观看| 亚洲日韩欧洲无码av夜夜摸| 怡红院免费的全部视频| 国产AV无码专区亚洲AV男同 | 91午夜精品亚洲一区二区三区| 中文字幕乱码免费视频| 免费h视频在线观看| 激情五月亚洲色图| 国产视频精品免费| 巨胸狂喷奶水视频www网站免费| 久久久久亚洲AV无码永不| 成人毛片免费网站| 国产99久久久久久免费看| 久久亚洲精品成人| 国产片免费福利片永久| 免费无码成人AV片在线在线播放| 中文在线免费观看| 成人无码区免费A∨直播| 国产福利免费视频 | 成人免费视频试看120秒| 毛片免费视频观看| 日韩精品免费一线在线观看| 91在线亚洲精品专区| 国产亚洲精品免费| 69免费视频大片| 久久久久久久免费视频| 久久国产乱子伦精品免费午夜| 国产成人精品日本亚洲18图| 亚洲人成综合在线播放| 亚洲色成人WWW永久在线观看 | 一级特黄特色的免费大片视频| www.亚洲成在线|