在网上看到一个笔试题,感觉挺有意思的,然后我尝试着解一解,欢迎各位大佬指正。

代码题(用 OOP 的思想编码,注意代码规范) 写出你的思路,最好有代码

用 php 写一个微波炉,注意物品正加热时不能开门,带皮带壳食物不能被加热。

感谢大佬们的建议,更进一步理解了 OOP,现更新第二版

第二版

<?php/** * Created by PhpStorm. * User: arun * Date: 2019-04-30 * Time: 16:10 *//** * 厨房用具 * Interface kitchenWare */interface kitchenWare {    /**     * 加工食材     * @param Food $food     * @return mixed     */    public function process(Food $food);    /**     * 是否正在加工     * @return mixed     */    public function hasProcess();}/** * 微波炉 * Class MicrowaveOven */class MicrowaveOven implements kitchenWare{    /**     * 是否加热中     * @var bool     */    protected $is_heat = false;    /**     * @param Food $food     * @return mixed|string     */    public function process(Food $food)    {        if ($this->hasProcess()) {            return '已有食物在加热,无法打开';        } else {            if ($food->hasShuck() || $food->hasPericarp()) {                return '食物带壳或者带皮,无法进行加热';            } else {                $this->is_heat = true;                return '食物加热中,加热完成即可取出';            }        }    }    /**     * 是否正在加工     * @return bool|mixed     */    public function hasProcess()    {        return $this->is_heat;    }}/** * 烤箱 * Class Oven */class Oven implements kitchenWare{    /**     * 是否烧烤中     * @var bool     */    protected $is_heat = false;    /**     * @param Food $food     * @return mixed|string     */    public function process(Food $food)    {        if ($this->is_heat) {            return '已有食物在烤制,无法打开';        } else {            if ($food->hasShuck()) {                return '食物带壳,无法进行烤制';            } else {                $this->is_heat = true;                return '食物烤制中,完成即可取出';            }        }    }    /**     * 是否正在加工     * @return bool|mixed     */    public function hasProcess()    {        return $this->is_heat;    }}/** * 食物 * Class Food */class Food{    /**     * 是否带壳     * @var bool     */    protected $is_shuck = false;    /**     * 是否带皮     * @var bool     */    protected $is_pericarp = false;    /**     * Food constructor.     * @param bool $is_shuck     * @param bool $is_pericarp     */    public function __construct(bool $is_shuck, bool $is_pericarp)    {        $this->is_shuck = $is_shuck;        $this->is_pericarp = $is_pericarp;    }    /**     * 判断是否带壳     * @return bool     */    public function hasShuck()    {        return $this->is_shuck;    }    /**     * 判断是否带皮     * @return bool     */    public function hasPericarp()    {        return $this->is_pericarp;    }}/** * 烹饪 * Class Cooking */class Cooking{    /**     * @var kitchenWare     */    protected $kitchenWare;    /**     * Cook constructor.     * @param kitchenWare $kitchenWare     */    public function __construct(kitchenWare $kitchenWare)    {        $this->kitchenWare = $kitchenWare;    }    /**     * 烹饪食物     * @param Food $food     * @return mixed     */    public function cooking(Food $food)    {        $data = $this->kitchenWare->process($food);        return $data;    }}/** * 微波炉加热 * @return mixed */function test(){    $cooking = new Cooking(new MicrowaveOven());    $food = new Food(false, false);    $result = $cooking->cooking($food);    $result2 = $cooking->cooking($food);    var_dump($result, $result2);}/** * 烤箱烤制 * @return mixed */function test2(){    $cooking = new Cooking(new Oven());    $food = new Food(false, true);    $result =  $cooking->cooking($food);    $result2 =  $cooking->cooking($food);    var_dump($result, $result2);}test();test2();

第一版

<?php/** * Created by PhpStorm. * User: arun * Date: 2019-04-30 * Time: 16:10 *//** * 厨房用具 * Interface kitchenWare */interface kitchenWare {    /**     * 加工食材     *     * @param $food     * @return mixed     */    public function process($food);}/** * 微波炉 * Class MicrowaveOven */class MicrowaveOven implements kitchenWare{    /**     * 是否加热中     * @var bool     */    protected $is_heat = false;    public function process($food)    {        if ($this->is_heat) {            return '已有食物在加热,无法打开';        } else {            if ($food['is_shuck'] || $food['is_pericarp']) {                return '食物带壳或者带皮,无法进行加热';            } else {                $this ->is_heat = true;                return '食物加热中,加热完成即可取出';            }        }    }}/** * 烤箱 * Class Oven */class Oven implements kitchenWare{    /**     * 是否烧烤中     * @var bool     */    protected $is_heat = false;    public function process($food)    {        if ($this->is_heat) {            return '已有食物在烤制,无法打开';        } else {            if ($food['is_shuck']) {                return '食物带壳,无法进行烤制';            } else {                $this ->is_heat = true;                return '食物烤制中,完成即可取出';            }        }    }}/** * 食物 * Class Food */class Food{    /**     * 是否带壳     * @var bool     */    protected $is_shuck = false;    /**     * 是否带皮     * @var bool     */    protected $is_pericarp = false;    /**     * Food constructor.     * @param bool $is_shuck     * @param bool $is_pericarp     */    public function __construct(bool $is_shuck, bool $is_pericarp)    {        $this->is_shuck = $is_shuck;        $this->is_pericarp = $is_pericarp;    }    public function getFood()    {        return [            'is_shuck' => $this->is_shuck,            'is_pericarp' => $this->is_pericarp,        ];    }}/** * 烹饪 * Class Cooking */class Cooking{    /**     * @var kitchenWare     */    protected $kitchenWare;    /**     * Cook constructor.     * @param kitchenWare $kitchenWare     */    public function __construct(kitchenWare $kitchenWare)    {        $this->kitchenWare = $kitchenWare;    }    /**     * 烹饪食物     * @param $food     * @return mixed     */    public function cooking($food)    {        $data = $this->kitchenWare->process($food);        return $data;    }}/** * 微波炉加热 * @return mixed */function test(){    $cooking = new Cooking(new MicrowaveOven());    $food = new Food(false, false);    $result = $cooking->cooking($food->getFood());    $result2 = $cooking->cooking($food->getFood());    var_dump($result, $result2);}/** * 烤箱烤制 * @return mixed */function test2(){    $cooking = new Cooking(new Oven());    $food = new Food(false, true);    $result =  $cooking->cooking($food->getFood());    $result2 =  $cooking->cooking($food->getFood());    var_dump($result, $result2);}test();test2();

更多相关文章

  1. 字体图标的引入和通过媒体查询改变导航样式
  2. HTML样式和常用选择器
  3. 字体图标的引用和自定义样式/媒体查询的使用
  4. 数据库的CURD操作、PDO本质与原理的学习
  5. CSS之伪类选择器和简单盒子简单案例
  6. 伪类选择器与盒模型常用属性
  7. 伪类选择器-结构伪类、根据位置选择匹配
  8. 7.4——常用标签与应用场景之表格与单元格
  9. css伪类选择器和盒模型

随机推荐

  1. android学习日记01-搭配开发环境
  2. Android 各版本Gradle离线下载
  3. android生命周期_Android活动生命周期–
  4. Android(安卓)API Demos笔记
  5. Android 实现开关灯效果
  6. android:shape的使用 (android用xml文件生
  7. Android android下的电话拨号器
  8. Android应用程序签名
  9. Android支付宝接口集成
  10. Android之拍照