代码题(用oop的思想编码,注意代码规范)
用java写一个微波炉,注意物品正加热时不能开门,带皮带壳食物不能被加热。
用Java写一个冰箱,注意家用冰箱多数分为冷冻柜和冷藏柜两个柜(冰箱随机)
如题,就是要用面向对象的方式写代码,我怎么看不出来怎么写,
比如第二题,我一个接口然后具体类实现接口完事,结果被鄙视,无情鄙视啊
这是某某的面试题,大家各抒己见!!!
------解决方案--------------------
要考虑的比较全的话还是有些工作的,随便写的可能有些地方有误
- Java code
public class MicoroWaveOven { private String doorState=null; private boolean isPowerOn=false; private Food cookingFood=null; public void fire(Food food){ if(!this.isPowerOn) return; if(food==null) return; if(this.doorState.equals("opend")){ alert("请先关门!"); return; } if(food.hasShell()){ alert("本产品不能烹饪带壳食物!"); openDoor(); return; } this.cookingFood=food; //to-do:另启动线程加热食物 } public void cookFood(){ if(this.cookingFood==null) return; while(cookingFood.getState()!=FoodState.COOKED){ cookingFood.changeState(); } openDoor(); } public void openDoor(){ if(cookingFood==null ||cookingFood.getState()==FoodState.COOKED){ this.doorState="opened"; }else{ alert("食物正在加热中请稍后"); } } public void on(){ this.isPowerOn=true; } public void off(){ this.isPowerOn=false; } public void closeDoor(){ this.doorState="closed"; } public boolean isDoorOpen(){ return this.doorState.equals("opened"); } public boolean isDoorClose(){ return this.doorState.equals("closed"); } public void alert(String message){ System.out.println("警告:"+message); }}interface Food{ public boolean hasShell(); public void changeState(); public FoodState getState();}enum FoodState{ RARE, HALF_RARE, COOKED, FIRED}
------解决方案--------------------
看着很有意思,请指教!
class Microwave {
int power;
int size;
int container;
int color;
boolean open
double time [] = {1,2,3,4,5,6,7,8,9,};
String model;
Person p;
public double timeMenu(double time) {
for(int i=0; i<time.length(); i++;) {
System.out.println("已工作时间:" + time[i] + "min");
heating(new Food());
}
}
public void heating(Food food) {
System.out.println("食物正在加热");
if(p.normal) {
System.out.println("I couldn't open the door");
} else {
System.out.println("疯子");
}
}
}
class Food {
boolean sikn_food = true;
int weigth;
int height;
int size;
int type;
}
public class Person {
boolean normal = true;
public boolean judgeFood(Food food) {
eye();
}
public void eye(Food food){
if(food == skin_food) {
System.out.println("这是有皮的食物,不可以在微波炉中加热");
break;
} else if(food != skin_food) {
System.out.println("这是无皮的食物,可以在微波炉中加热");
handTime();
}
}
public void handTime() {
timeMenu();
}
public static void main(String[] args) {
Person p = new Person();
p.judgeFood();
}
------解决方案--------------------
关于冰箱的问题说的不清楚,我发表一下我对微波炉的看法,我觉得它真的能看出一个程序员的水平高低
我们来分析一下这个问题
用java写一个微波炉,注意物品正加热时不能开门,带皮带壳食物不能被加热。