您的位置:首页 > 其它

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

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

viewsource

print?

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
*@paramarray$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
*@authorSamuel
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
*@seeWife::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
*@seeWife::WashClothes()
090
*/
091
function
WashClothes(){
092
//调用Wife类洗衣服方法
093
parent::WashClothes();
094
}
095
096
/**
097
*做家务
098
*@seeWife::DoHouseholdDuties()
099
*/
100
function
DoHouseholdDuties(){
101
//调用Wife类做家务方法
102
parent::DoHouseholdDuties();
103
}
104
}
105
?>
然后是接口的方式:

viewsource

print?

001
<?php
002
/**
003
*接口模式老婆
004
*Wife接口
005
*/
006
interface
Wife{
007
/**
008
*煮饭
009
*@paramarray$howToCook菜的做法
010
*@paramarray$vegetableArray需买的菜的数组
011
*/
012
function
Cook(
$howToCook
,
$vegetableArray
){
013
}
014
015
/**
016
*买菜
017
*@paramarray$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
*@authorSamuel
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
*@paramarray$howToCook菜的做法
065
*@paramarray$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
*@paramarray$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
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: