• <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)用父親的.(沒有就默認(rèn)調(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 閱讀(2808) 評論(0)  編輯 收藏 引用 所屬分類: c++
            久久不见久久见免费影院www日本| 囯产极品美女高潮无套久久久| 精品九九久久国内精品| 久久青青草原综合伊人| 久久99亚洲综合精品首页| 性做久久久久久久久| 久久久久亚洲精品日久生情| 久久亚洲欧美国产精品| 精品久久久久久无码人妻热 | 91精品免费久久久久久久久| 久久精品国产一区二区三区不卡 | 久久精品a亚洲国产v高清不卡| 中文字幕成人精品久久不卡| 91麻豆国产精品91久久久| 久久精品国产亚洲av麻豆色欲| 久久久无码精品午夜| 精品乱码久久久久久久| 波多野结衣久久精品| 久久久久久久国产免费看| 国产精品免费福利久久| 四虎国产精品成人免费久久| 久久精品成人免费国产片小草| 久久精品国产亚洲AV高清热| 久久天天躁夜夜躁狠狠| 久久精品一区二区三区中文字幕 | 精品久久久久久无码人妻热| 国产精品一区二区久久国产| 国产精品99久久久精品无码| 久久无码人妻精品一区二区三区| 成人久久综合网| 久久婷婷激情综合色综合俺也去| 久久亚洲日韩看片无码| 欧美伊人久久大香线蕉综合69| 久久综合狠狠综合久久激情 | 精品永久久福利一区二区| 久久久久久久人妻无码中文字幕爆| 久久99热这里只有精品66| 日韩人妻无码一区二区三区久久99| 四虎久久影院| 香蕉久久夜色精品国产2020| 久久这里的只有是精品23|