用 PHP 写一个微波炉

用 PHP 写一个微波炉

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

代码题(用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
*/
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();
#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×