• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ivy-jie

            progress ...

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              9 Posts :: 41 Stories :: 6 Comments :: 0 Trackbacks

            子類繼承和調(diào)用父類的構(gòu)造方法

             

            1.如果子類沒有定義構(gòu)造方法,則調(diào)用父類的無參數(shù)的構(gòu)造方法,.

            2.如果子類定義了構(gòu)造方法,不論是無參數(shù)還是帶參數(shù),在創(chuàng)建子類的對象的時候,首先執(zhí)行父類無參數(shù)的構(gòu)造方法,然后執(zhí)行自己的構(gòu)造方法。

            3.如果子類調(diào)用父類帶參數(shù)的構(gòu)造方法,可以通過super(參數(shù))調(diào)用所需要的父類的構(gòu)造方法,切該語句做為子類構(gòu)造方法中的第一條語句

            4.如果某個構(gòu)造方法調(diào)用類中的其他的構(gòu)造方法,則可以用this(參數(shù)),切該語句放在構(gòu)造方法的第一條.

            說白了:原則就是,先調(diào)用父親的.(沒有就默認調(diào),有了就按有的調(diào),反正只要有一個就可以了.)

            package test;

            class Father{

            String s = "Run constructor method of Father";

            public Father(){

               System.out.println(s);

            }

            public Father(String str){

               s= str;

               System.out.println(s);

            }

            }

            class Son extends Father{

            String s= "Run constructor method of son";

            public Son(){

               //實際上在這里加上super(),和沒加是一個樣的

               System.out.println(s);

            }

            public Son(String str){

               this();//這里調(diào)用this()表示調(diào)用本類的Son(),因為Son()中有了一個super()了,所以這里不能再加了。

               s = str;

               System.out.println(s);

            }

            public Son(String str1, String str2){

               super(str1+" "+str2);//因為這里已經(jīng)調(diào)用了一個父類的帶參數(shù)的super("---")了,所以不會再自動調(diào)用了無參數(shù)的了。

               s = str1;

               System.out.println(s);

            }

            }

            public class MyClass9 {

            public static void main(String[] args){

               Father obfather1 = new Father();

               Father obfather2 = new Father("Hello Father");

               Son obson1 = new Son();

               Son obson2 = new Son("hello son");

               Son obson3 = new Son("hello son","hello father");

              

            }

            }

            ===============

            結(jié)果:

            Run constructor method of Father

            Hello Father

            Run constructor method of Father

            Run constructor method of son

            Run constructor method of Father

            Run constructor method of son

            hello son

            hello son hello father

            hello son

            posted on 2009-05-23 00:27 ivy-jie 閱讀(2845) 評論(0)  編輯 收藏 引用 所屬分類: c++
            亚洲一区中文字幕久久| 97超级碰碰碰久久久久| 人人狠狠综合久久亚洲88| 色妞色综合久久夜夜| 青青草原精品99久久精品66| yy6080久久| 久久精品一本到99热免费| 热久久视久久精品18| 精品国产乱码久久久久久人妻| 久久人人爽人人爽人人片AV东京热 | 久久无码人妻精品一区二区三区| 热re99久久精品国产99热| 91超碰碰碰碰久久久久久综合| 久久久久久久尹人综合网亚洲| 国产L精品国产亚洲区久久| 91性高湖久久久久| 无码人妻少妇久久中文字幕| 婷婷国产天堂久久综合五月| 久久精品中文騷妇女内射| 国产精品女同久久久久电影院| 四虎国产精品免费久久5151| 久久国产乱子伦精品免费午夜| 2021国产精品久久精品| 久久久久无码精品国产| 99久久国产免费福利| 思思久久99热只有频精品66| 精品久久久久久久无码| 91精品国产色综合久久| 精品久久久久中文字幕一区| 一本久道久久综合狠狠躁AV| 亚洲AV无码久久| 久久播电影网| 午夜人妻久久久久久久久| 久久97久久97精品免视看秋霞| 久久免费看黄a级毛片| 日本免费久久久久久久网站| 亚洲va中文字幕无码久久不卡| 99久久精品九九亚洲精品| 久久午夜无码鲁丝片秋霞| 久久国产乱子精品免费女| 婷婷久久五月天|