您的位置:首页 > 其它

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

2011-03-30 15:19 351 查看

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

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

2011-03-25

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

先是类的方式。

View Code

<?php
/**
* 接口模式老婆
* Wife接口
*/
interface Wife {
/**
* 煮饭
* @param array $howToCook 菜的做法
* @param array $vegetableArray 需买的菜的数组
*/
function Cook($howToCook, $vegetableArray) {
}

/**
* 买菜
* @param array $vegetableArray 菜名数组
*/
function BuyVegetables($vegetableArray) {
}

/**
* 洗衣服
*/
function WashClothes() {
}

/**
* 做家务
*/
function DoHouseholdDuties() {
}
}

/**
* I类 实现Wife接口
* @author Samuel
*/
class I implements Wife {

/**
*打游戏
*/
function PlayGames() {
"打游戏";
}

/**
* 打篮球
*/
function PlayBasketball() {
"打篮球";
}

/**
* 看电视
*/
function WatchTV() {
"看电视";
}

/**
* 煮饭
* @param array $howToCook 菜的做法
* @param array $vegetableArray 需买的菜的数组
*/
public function Cook($howToCook, $vegetableArray) {
$this->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_擦桌子";
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: