Home > Backend Development > PHP Tutorial > Use classes and interfaces to describe ordinary daily life_PHP tutorial

Use classes and interfaces to describe ordinary daily life_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:33:08
Original
952 people have browsed it

In object-oriented programming, classes and interfaces are the two most basic concepts. Let's write a simple program to demonstrate how to write programs using base classes and interfaces. The program is very simple, no need to explain too much, just jump into the code. The majority of programmer brothers must be able to understand what it means.

The first is the class method.

<?php
/**
 * 类模式老婆
 * Wife基类
 */
class Wife {
    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_擦桌子";
    }
}
/**
 * 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 ();
    }
}
?>
Copy after login

Then the interface method:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752531.htmlTechArticleIn object-oriented programming, classes and interfaces are the two most basic concepts. Let's write a simple program to demonstrate how to write programs using base classes and interfaces. The procedure is very simple, no need to go through...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template