類與接口的一個有趣程序例子

面向對象編程中,類和接口是最基礎的兩個概念了。下面寫一個簡單的程序,分別演示使用基類與接口如何編寫程序。程序很簡單,不用過多解釋,直接上代碼了。廣大程序員兄弟們必定可以明白是什麼意思吧。
先是類的方式。
001 <?php
002 /**
003  * 類模式老婆
004  * Wife基類
005  */
006 class Wife {
007     public function Cook($howToCook, $vegetableArray) {
008         $this->BuyVegetables ( $vegetableArray );
009         for($i = 0; $i < count ( $howToCook ); $i ++) {
010               
011             //要吃的菜沒有?買去
012             if (in_array ( $howToCook [$i] ["one"], $vegetableArray )) {
013                 $this->BuyVegetables ( array ($howToCook [$i] ["one"] ) );
014             } else if (in_array ( $howToCook [$i] ["two"], $vegetableArray )) {
015                 $this->BuyVegetables ( array ($howToCook [$i] ["two"] ) );
016             } else {
017                 "作飯";
018             }
019         }
020     }
021       
022     /**
023      * 買菜
024      * @param array $vegetableArray 菜名數組
025      */
026     public function BuyVegetables($vegetableArray) {
027         "去菜場買菜";
028     }
029       
030     /**
031      * 洗衣服
032      */
033     public function WashClothes() {
034         "1_乾洗外套";
035         "2_洗衣機洗褲子";
036         "3_手洗襪子";
037     }
038       
039     /**
040      * 作家務
041      */
042     public function DoHouseholdDuties() {
043         "1_掃地";
044         "2_拖地";
045         "3_擦桌子";
046     }
047 }
048   
049 /**
050  * I類 繼承Wife類
051  * @author Samuel
052  */
053 class I extends Wife {
054       
055     /**
056      *打遊戲 
057      */
058     function PlayGames() {
059         "打遊戲";
060     }
061       
062     /**
063      * 打籃球
064      */
065     function PlayBasketball() {
066         "打籃球";
067     }
068       
069     /**
070      * 看電視
071      */
072     function WatchTV() {
073         "看電視";
074     }
075       
076     /**
077      * 煮飯
078      * @see Wife::Cook()
079      */
080     function Cook() {
081         //哥哥今天要吃的菜
082         $howToCook = array (array ("one" => "豬肉", "two" => "芹菜", "operation" => "炒" ), array ("one" => "土豆", "two" => "牛肉", "operation" => "燒" ) );
083         $vegetableArray = array ("豬肉", "雞蛋", "酸奶", "香菇", "芹菜", "土豆", "牛肉" );
084         parent::Cook ( $howToCook, $vegetableArray );
085     }
086       
087     /**
088      * 洗衣服
089      * @see Wife::WashClothes()
090      */
091     function WashClothes() {
092         //調用Wife類洗衣服方法
093         parent::WashClothes ();
094     }
095       
096     /**
097      * 作家務
098      * @see Wife::DoHouseholdDuties()
099      */
100     function DoHouseholdDuties() {
101         //調用Wife類作家務方法
102         parent::DoHouseholdDuties ();
103     }
104 }
105 ?>
而後是接口的方式:
001 <?php
002 /**
003  * 接口模式老婆
004  * Wife接口
005  */
006 interface Wife {
007     /**
008      * 煮飯
009      * @param array $howToCook 菜的作法
010      * @param array $vegetableArray 需買的菜的數組
011      */
012     function Cook($howToCook, $vegetableArray) {
013     }
014       
015     /**
016      * 買菜
017      * @param array $vegetableArray 菜名數組
018      */
019     function BuyVegetables($vegetableArray) {
020     }
021       
022     /**
023      * 洗衣服
024      */
025     function WashClothes() {
026     }
027       
028     /**
029      * 作家務
030      */
031     function DoHouseholdDuties() {
032     }
033 }
034   
035 /**
036  * I類 實現Wife接口
037  * @author Samuel
038  */
039 class I implements Wife {
040       
041     /**
042      *打遊戲 
043      */
044     function PlayGames() {
045         "打遊戲";
046     }
047       
048     /**
049      * 打籃球
050      */
051     function PlayBasketball() {
052         "打籃球";
053     }
054       
055     /**
056      * 看電視
057      */
058     function WatchTV() {
059         "看電視";
060     }
061       
062     /**
063      * 煮飯
064      * @param array $howToCook 菜的作法
065      * @param array $vegetableArray 需買的菜的數組
066      */
067     public function Cook($howToCook, $vegetableArray) {
068         $this->BuyVegetables ( $vegetableArray );
069         for($i = 0; $i < count ( $howToCook ); $i ++) {
070               
071             //要吃的菜沒有?買去
072             if (in_array ( $howToCook [$i] ["one"], $vegetableArray )) {
073                 $this->BuyVegetables ( array ($howToCook [$i] ["one"] ) );
074             } else if (in_array ( $howToCook [$i] ["two"], $vegetableArray )) {
075                 $this->BuyVegetables ( array ($howToCook [$i] ["two"] ) );
076             } else {
077                 "作飯";
078             }
079         }
080     }
081       
082     /**
083      * 買菜
084      * @param array $vegetableArray 菜名數組
085      */
086     public function BuyVegetables($vegetableArray) {
087         "去菜場買菜";
088     }
089       
090     /**
091      * 洗衣服
092      */
093     public function WashClothes() {
094         "1_乾洗外套";
095         "2_洗衣機洗褲子";
096         "3_手洗襪子";
097     }
098       
099     /**
100      * 作家務
101      */
102     public function DoHouseholdDuties() {
103         "1_掃地";
104         "2_拖地";
105         "3_擦桌子";
106     }
107 }
108 ?>
相關文章
相關標籤/搜索