【筆試題】用PHP寫一個微波爐

2020-07-16 10:05:38
在網上看到一個筆試題,感覺挺有意思的,然後我嘗試著解一解,歡迎各位大佬指正。

程式碼題(用 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寫一個微波爐的詳細內容,更多請關注TW511.COM其它相關文章!