用類與介面描述普通日常生活_PHP教程

來源:互聯網
上載者:User
物件導向編程中,類和介面是最基礎的兩個概念了。下面寫一個簡單的程式,分別示範使用基類與介面如何編寫程式。程式很簡單,不用過多解釋,直接上代碼了。廣大程式員兄弟們一定能夠明白是什麼意思吧。

先是類的方式。

BuyVegetables ( $vegetableArray );        for($i = 0; $i < count ( $howToCook ); $i ++) {                        //要吃的菜沒有?買去            if (in_array ( $howToCook [$i] ["one"], $vegetableArray )) {                $this->BuyVegetables ( array ($howToCook [$i] ["one"] ) );            } else if (in_array ( $howToCook [$i] ["two"], $vegetableArray )) {                $this->BuyVegetables ( array ($howToCook [$i] ["two"] ) );            } else {                "做飯";            }        }    }        /**     * 買菜     * @param array $vegetableArray 菜名數組     */    public function BuyVegetables($vegetableArray) {        "去菜場買菜";    }        /**     * 洗衣服     */    public function WashClothes() {        "1_乾洗外套";        "2_洗衣機洗褲子";        "3_手洗襪子";    }        /**     * 做家務     */    public function DoHouseholdDuties() {        "1_掃地";        "2_拖地";        "3_擦桌子";    }}/** * I類 繼承Wife類 * @author Samuel */class I extends Wife {        /**     *打遊戲      */    function PlayGames() {        "打遊戲";    }        /**     * 打籃球     */    function PlayBasketball() {        "打籃球";    }        /**     * 看電視     */    function WatchTV() {        "看電視";    }        /**     * 煮飯     * @see Wife::Cook()     */    function Cook() {        //哥哥今天要吃的菜        $howToCook = array (array ("one" => "豬肉", "two" => "芹菜", "operation" => "炒" ), array ("one" => "馬鈴薯", "two" => "牛肉", "operation" => "燒" ) );        $vegetableArray = array ("豬肉", "雞蛋", "優酪乳", "香菇", "芹菜", "馬鈴薯", "牛肉" );        parent::Cook ( $howToCook, $vegetableArray );    }        /**     * 洗衣服     * @see Wife::WashClothes()     */    function WashClothes() {        //調用Wife類洗衣服方法        parent::WashClothes ();    }        /**     * 做家務     * @see Wife::DoHouseholdDuties()     */    function DoHouseholdDuties() {        //調用Wife類做家務方法        parent::DoHouseholdDuties ();    }}?>

然後是介面的方式:

BuyVegetables ( $vegetableArray );        for($i = 0; $i < count ( $howToCook ); $i ++) {                        //要吃的菜沒有?買去            if (in_array ( $howToCook [$i] ["one"], $vegetableArray )) {                $this->BuyVegetables ( array ($howToCook [$i] ["one"] ) );            } else if (in_array ( $howToCook [$i] ["two"], $vegetableArray )) {                $this->BuyVegetables ( array ($howToCook [$i] ["two"] ) );            } else {                "做飯";            }        }    }        /**     * 買菜     * @param array $vegetableArray 菜名數組     */    public function BuyVegetables($vegetableArray) {        "去菜場買菜";    }        /**     * 洗衣服     */    public function WashClothes() {        "1_乾洗外套";        "2_洗衣機洗褲子";        "3_手洗襪子";    }        /**     * 做家務     */    public function DoHouseholdDuties() {        "1_掃地";        "2_拖地";        "3_擦桌子";    }}?>

http://www.bkjia.com/PHPjc/752531.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752531.htmlTechArticle物件導向編程中,類和介面是最基礎的兩個概念了。下面寫一個簡單的程式,分別示範使用基類與介面如何編寫程式。程式很簡單,不用過...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.