拟实现电风扇,可以调3档速度(慢速、中速、快速);开关按钮;定时吹风;描述风扇的扇叶大小、颜色等。 设计Fan类,属性包括:3个常量SLOW(1)、MEDIUM(2)、FAST(3)代表风扇的速度;1个int属性speed指定速度,默认值为SLOW;1个boolean属性on指定开关机,
时间: 2024-01-22 19:04:25 浏览: 215
好的,根据您的需求,设计的Fan类如下:
```java
public class Fan {
private final int SLOW = 1;
private final int MEDIUM = 2;
private final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "blue";
public Fan() {
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
if (on) {
return "Fan speed: " + speed + ", color: " + color + ", radius: " + radius;
} else {
return "Fan is off" + ", color: " + color + ", radius: " + radius;
}
}
}
```
以上代码实现了风扇的速度调节、开关机、风扇扇叶大小、颜色等功能,并提供了toString方法以便于输出风扇的状态。您可以在其他类中实例化Fan类并调用其方法来操作风扇。
阅读全文