在网上看到一个笔试题,感觉挺有意思的,然后我尝试着写一写,欢迎各位大佬指正。
代码题(用OOP的思想编码,注意代码规范) 写出你的思路,最好有代码
用php写一个微波炉,注意物品正加热时不能开门,带皮带壳食物不能被加热。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| <?php
interface kitchenWare {
public function process(Food $food);
public function hasProcess(); }
class MicrowaveOven implements kitchenWare {
protected $is_heat = false;
public function process(Food $food) { if ($this->hasProcess()) { return '已有食物在加热,无法打开'; } else { if ($food->hasShuck() || $food->hasPericarp()) { return '食物带壳或者带皮,无法进行加热'; } else { $this->is_heat = true; return '食物加热中,加热完成即可取出'; } } }
public function hasProcess() { return $this->is_heat; } }
class Oven implements kitchenWare {
protected $is_heat = false;
public function process(Food $food) { if ($this->is_heat) { return '已有食物在烤制,无法打开'; } else { if ($food->hasShuck()) { return '食物带壳,无法进行烤制'; } else { $this->is_heat = true; return '食物烤制中,完成即可取出'; } } }
public function hasProcess() { return $this->is_heat; } }
class Food {
protected $is_shuck = false;
protected $is_pericarp = false;
public function __construct(bool $is_shuck, bool $is_pericarp) { $this->is_shuck = $is_shuck; $this->is_pericarp = $is_pericarp; }
public function hasShuck() { return $this->is_shuck; }
public function hasPericarp() { return $this->is_pericarp; } }
class Cooking {
protected $kitchenWare;
public function __construct(kitchenWare $kitchenWare) { $this->kitchenWare = $kitchenWare; }
public function cooking(Food $food) { $data = $this->kitchenWare->process($food); return $data; } }
function test() { $cooking = new Cooking(new MicrowaveOven()); $food = new Food(false, false); $result = $cooking->cooking($food); $result2 = $cooking->cooking($food); var_dump($result, $result2); }
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();
|