类与接口的一个有趣程序例子

类与接口的一个有趣程序例子

转自http://www.nowamagic.net/php/php_InterestedExampleForClassInterface.php

2011-03-25

面向对象编程中,类和接口是最基础的两个概念了。下面写一个简单的程序,分别演示使用基类与接口如何编写程序。程序很简单,不用过多解释,直接上代码了。广大程序员兄弟们一定能够明白是什么意思吧。

先是类的方式。

View Code
1 <?php
2  /**
3 * 类模式老婆
4 * Wife基类
5 */
6 class Wife {
7 public function Cook($howToCook, $vegetableArray) {
8 $this->BuyVegetables ( $vegetableArray );
9 for($i = 0; $i < count ( $howToCook ); $i ++) {
10
11 //要吃的菜没有?买去
12 if (in_array ( $howToCook [$i] ["one"], $vegetableArray )) {
13 $this->BuyVegetables ( array ($howToCook [$i] ["one"] ) );
14 } else if (in_array ( $howToCook [$i] ["two"], $vegetableArray )) {
15 $this->BuyVegetables ( array ($howToCook [$i] ["two"] ) );
16 } else {
17 "做饭";
18 }
19 }
20 }
21
22 /**
23 * 买菜
24 * @param array $vegetableArray 菜名数组
25 */
26 public function BuyVegetables($vegetableArray) {
27 "去菜场买菜";
28 }
29
30 /**
31 * 洗衣服
32 */
33 public function WashClothes() {
34 "1_干洗外套";
35 "2_洗衣机洗裤子";
36 "3_手洗袜子";
37 }
38
39 /**
40 * 做家务
41 */
42 public function DoHouseholdDuties() {
43 "1_扫地";
44 "2_拖地";
45 "3_擦桌子";
46 }
47 }
48
49 /**
50 * I类 继承Wife类
51 * @author Samuel
52 */
53 class I extends Wife {
54
55 /**
56 *打游戏
57 */
58 function PlayGames() {
59 "打游戏";
60 }
61
62 /**
63 * 打篮球
64 */
65 function PlayBasketball() {
66 "打篮球";
67 }
68
69 /**
70 * 看电视
71 */
72 function WatchTV() {
73 "看电视";
74 }
75
76 /**
77 * 煮饭
78 * @see Wife::Cook()
79 */
80 function Cook() {
81 //哥哥今天要吃的菜
82 $howToCook = array (array ("one" => "猪肉", "two" => "芹菜", "operation" => "" ), array ("one" => "土豆", "two" => "牛肉", "operation" => "" ) );
83 $vegetableArray = array ("猪肉", "鸡蛋", "酸奶", "香菇", "芹菜", "土豆", "牛肉" );
84 parent::Cook ( $howToCook, $vegetableArray );
85 }
86
87 /**
88 * 洗衣服
89 * @see Wife::WashClothes()
90 */
91 function WashClothes() {
92 //调用Wife类洗衣服方法
93 parent::WashClothes ();
94 }
95
96 /**
97 * 做家务
98 * @see Wife::DoHouseholdDuties()
99 */
100 function DoHouseholdDuties() {
101 //调用Wife类做家务方法
102 parent::DoHouseholdDuties ();
103 }
104 }
105 ?>

然后是接口的方式:

View Code
1 <?php
2 /**
3 * 接口模式老婆
4 * Wife接口
5 */
6 interface Wife {
7 /**
8 * 煮饭
9 * @param array $howToCook 菜的做法
10 * @param array $vegetableArray 需买的菜的数组
11 */
12 function Cook($howToCook, $vegetableArray) {
13 }
14
15 /**
16 * 买菜
17 * @param array $vegetableArray 菜名数组
18 */
19 function BuyVegetables($vegetableArray) {
20 }
21
22 /**
23 * 洗衣服
24 */
25 function WashClothes() {
26 }
27
28 /**
29 * 做家务
30 */
31 function DoHouseholdDuties() {
32 }
33 }
34
35 /**
36 * I类 实现Wife接口
37 * @author Samuel
38 */
39 class I implements Wife {
40
41 /**
42 *打游戏
43 */
44 function PlayGames() {
45 "打游戏";
46 }
47
48 /**
49 * 打篮球
50 */
51 function PlayBasketball() {
52 "打篮球";
53 }
54
55 /**
56 * 看电视
57 */
58 function WatchTV() {
59 "看电视";
60 }
61
62 /**
63 * 煮饭
64 * @param array $howToCook 菜的做法
65 * @param array $vegetableArray 需买的菜的数组
66 */
67 public function Cook($howToCook, $vegetableArray) {
68 $this->BuyVegetables ( $vegetableArray );
69 for($i = 0; $i < count ( $howToCook ); $i ++) {
70
71 //要吃的菜没有?买去
72 if (in_array ( $howToCook [$i] ["one"], $vegetableArray )) {
73 $this->BuyVegetables ( array ($howToCook [$i] ["one"] ) );
74 } else if (in_array ( $howToCook [$i] ["two"], $vegetableArray )) {
75 $this->BuyVegetables ( array ($howToCook [$i] ["two"] ) );
76 } else {
77 "做饭";
78 }
79 }
80 }
81
82 /**
83 * 买菜
84 * @param array $vegetableArray 菜名数组
85 */
86 public function BuyVegetables($vegetableArray) {
87 "去菜场买菜";
88 }
89
90 /**
91 * 洗衣服
92 */
93 public function WashClothes() {
94 "1_干洗外套";
95 "2_洗衣机洗裤子";
96 "3_手洗袜子";
97 }
98
99 /**
100 * 做家务
101 */
102 public function DoHouseholdDuties() {
103 "1_扫地";
104 "2_拖地";
105 "3_擦桌子";
106 }
107 }
108 ?>
原文地址:https://www.cnblogs.com/tenny/p/1999941.html