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

藏色散人
發布: 2023-04-06 21:02:02
轉載
3702 人瀏覽過

在網路上看到一個筆試題,感覺蠻有趣的,然後我嘗試著解一解,歡迎各位大佬指正。

程式碼題(用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 &#39;已有食物在加热,无法打开&#39;;
        } else {
            if ($food->hasShuck() || $food->hasPericarp()) {
                return &#39;食物带壳或者带皮,无法进行加热&#39;;
            } else {
                $this->is_heat = true;
                return &#39;食物加热中,加热完成即可取出&#39;;
            }
        }
    }
    /**
     * 是否正在加工
     * @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 &#39;已有食物在烤制,无法打开&#39;;
        } else {
            if ($food->hasShuck()) {
                return &#39;食物带壳,无法进行烤制&#39;;
            } else {
                $this->is_heat = true;
                return &#39;食物烤制中,完成即可取出&#39;;
            }
        }
    }
    /**
     * 是否正在加工
     * @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 &#39;已有食物在加热,无法打开&#39;;
        } else {
            if ($food[&#39;is_shuck&#39;] || $food[&#39;is_pericarp&#39;]) {
                return &#39;食物带壳或者带皮,无法进行加热&#39;;
            } else {
                $this ->is_heat = true;
                return &#39;食物加热中,加热完成即可取出&#39;;
            }
        }
    }
}
/**
 * 烤箱
 * Class Oven
 */
class Oven implements kitchenWare
{
    /**
     * 是否烧烤中
     * @var bool
     */
    protected $is_heat = false;
    public function process($food)
    {
        if ($this->is_heat) {
            return &#39;已有食物在烤制,无法打开&#39;;
        } else {
            if ($food[&#39;is_shuck&#39;]) {
                return &#39;食物带壳,无法进行烤制&#39;;
            } else {
                $this ->is_heat = true;
                return &#39;食物烤制中,完成即可取出&#39;;
            }
        }
    }
}
/**
 * 食物
 * 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 [
            &#39;is_shuck&#39; => $this->is_shuck,
            &#39;is_pericarp&#39; => $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中文網其他相關文章!

相關標籤:
來源:learnku.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板