• 技术文章 >php教程 >php手册

    用类与接口描述普通日常生活

    2016-06-13 09:38:23原创435

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

    先是类的方式。

    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_擦桌子";
        }
    }
    ?>
    

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:PHP 接口
    上一篇:编写一个简单的PHP操作提示类 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • php利用新浪接口查询ip获取地理位置• php mysql 数据库类• PHP代码:Http断点续传的实现例子• 聊天室php&mysql(二)• 基于php实现七牛抓取远程图片
    1/1

    PHP中文网