【笔试题】用PHP写一个微波炉-php教程

资源魔 37 0
正在网上看到一个口试题,觉得挺无意思的,而后我测验考试着解一解,欢送列位年夜佬斧正。

代码题(用 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();

以上就是【口试题】用PHP写一个微波炉的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 微波炉

抱歉,评论功能暂时关闭!