<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                interface? 接口類:?**成員方法必須聲明為public訪問權限,有定義但沒有具體實現,可以聲明常量但無法聲明屬性,無法實例化,只能被其他類implements(實現)的特殊類.只能extends(繼承)接口,但可以繼承多個interface(接口)?.** **1.接口中定義的所有方法都必須沒有實現,只有定義** ~~~ <?php //錯誤示例:?方法進行了實現 interface?human?{ ????public?function?walk(){ ????????//?todo ????} ???? ????public?function?run(){ ????????//?todo ????} } ~~~ ( ! )?Fatal error: Interface function human::walk() cannot contain body in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?81 **2.接口中所有方法的訪問修飾符都必須是public** ~~~ <?php //錯誤示例:?方法訪問權限修飾符不是public interface?human?{ ????protected?function?walk(); ???? ????private?function?run(); } ~~~ ( ! )?Fatal error: Access type for interface method human::walk() must be omitted in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?79 ~~~ <?php //正確示例:?方法訪問權限修飾符必須都是public,且方法只有定義沒有實現. interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } ~~~ **3.可以定義常量,但不能定義屬性** ~~~ <?php //錯誤示例:?定義屬性 interface?mp4?{ ????$name?=?'mp4'; ????public?function?vedio(); ????public?function?music(); } ~~~ ( ! )?Parse error: syntax error, unexpected '$name' (T\_VARIABLE), expecting function (T\_FUNCTION) in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?80 ~~~ <?php //正確示例:?定義常量 interface?mp4?{ ????const?name?=?'mp4'; ????public?function?vedio(); ????public?function?music(); } ~~~ **4.不能被實例化,只能被其他類實現** ~~~ <?php //錯誤示例:?實例化 interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } $human?=?new?human(); ~~~ ( ! )?Fatal error: Cannot instantiate interface human in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?83 ~~~ <?php //錯誤示例:?不是所有的方法都被實現.teacher::run()沒有方法體 interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } //$human?=?new?human(); class?teacher?implements?human?{ ????public?function?walk(){ ????????echo?"teacher?walk"; ????} ???? ????public?function?run(); } ~~~ ( ! )?Fatal error: Non-abstract method teacher::run() must contain body in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?90 ~~~ <?php //錯誤示例:?不是所有的方法都被實現.human::walk沒有被實現 interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } //$human?=?new?human(); class?teacher?implements?human?{ ????public?function?run(){ ????????echo?"teacher?run"; ????}; } ~~~ ( ! )?Fatal error: Class teacher contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (human::walk) in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?92 ~~~ <?php //正確示例:?所有的方法都被實現. interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } //$human?=?new?human(); class?teacher?implements?human?{ ????public?function?walk(){ ????????echo?"teacher?walk"; ????} ????public?function?run(){ ????????echo?"teacher?run"; ????}; ????//?還有有自己的特殊行為 ????public?function?teach(){ ????????echo?"teacher?teach"; ????} } $teacher?=?new?teacher(); $teacher->walk(); $teacher->run(); $teacher->teach(); ~~~ **5.可以extends(繼承)其他接口,最后的類implements(實現)接口時,需要將繼承鏈上的所有接口的抽象方法都實現** ~~~ <?php //錯誤示例:?沒有將繼承鏈上的接口的方法都實現 interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } interface?male?extends?human?{ ????public?function?showSelf(); } class?man?implements?male?{ ???? } $man?=?man(); ~~~ Fatal error: Class man contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (male::showSelf, human::walk, human::run) in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?90 ~~~ <?php //正確示例:?繼承鏈上的接口的方法都實現 interface?human?{ ????public?function?walk(); ???? ????public?function?run(); } interface?male?extends?human?{ ????public?function?showSelf(); } class?man?implements?male?{ ???? } class?man?implements?male?{ ????public?function?walk(){ ????????echo?"man?wallk"; ????} ???? ????public?function?run(){ ????????echo?"man?run"; ????} ???? ????public?function?showSelf(){ ????????echo?"man?show?himself"; ????} } $man?=?new?man(); $man->walk(); $man->run(); $man->showSelf(); ~~~ **6.可以繼承多個接口,多個被繼承的接口用逗號隔開,最后的類implements(實現)接口時,需要將繼承鏈上的所有接口的抽象方法都實現** ~~~ <?php //錯誤示例:?沒有將繼承鏈上的接口的方法都實現 interface?mp3?{ ????public?function?music(); } interface?phone?{ ????public?function?call(); } interface?mobilephone?extends?mp3,?phone?{ ????public?function?note(); } class?iphone?implements?mobilephone?{ ???? } $iphone?=?new?iphone(); ~~~ ( ! )?Fatal error: Class iphone contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (mobilephone::note, mp3::music, phone::call) in F:\\wamp\\www\\blog.54skyer.cn\\index.php on line?92 ~~~ <?php //正確示例:?將繼承鏈上的接口的方法都實現 interface?mp3?{ ????public?function?music(); } interface?phone?{ ????public?function?call(); } interface?mobilephone?extends?mp3,?phone?{ ????public?function?note(); } class?iphone?implements?mobilephone?{ ????public?function?music(){ ????echo?'play?a?music'; ????} ????public?function?call(){ ????echo?'make?a?call'; ????} ???? ????public?function?note(){ ????echo?'write?a?note'; ????} } $iphone?=?new?iphone(); $iphone->music(); $iphone->call(); $iphone->note(); ~~~ 總結:?接口更多是定義一個標準,用來規范屬于這個抽象范圍內的子類或者實現類.如果希望代碼工程是標準規范可控,應該業務模型階段把接口都定義好,寫業務邏輯時只需要關注實現就可以了,方法屬性命名都可以在定義接口類時定義好. 注: implements(實現)接口的第一個普通類\[即可以是普通的class也可以是abstract?class\],所有方法訪問權限必須為public,方法的實現必須保持參數數量一致,參數名稱可以不同. 有關abstract(抽象類)的相關只是見另一篇博文 =>?[類相關的關鍵字 - abstrct](http://www.php.cn/blog/admin/article_edit/6579.html "抽象類")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看