package pack2;
public class Fan {
public final int SLOW = 1, MEDIUM = 2, FAST = 3;//慢速、中速、高速
private int speed; //速度
private boolean on; //状态(打开/关闭)
private double radius; //半径
private String color; //颜色
public Fan() {
speed = SLOW;
radius = 5;
color = "blue";
}
@Override /**返回风扇描述的字符串*/
public String toString() {
if(on) //如果风扇打开,返回速度、颜色、半径的字符串
return "Speed: " + speed + "\nColor: " + color + "\nRadius: " + radius;
else //否则,输出关闭信息和颜色、半径
return "fan is off\nColor: " + color + "\nRadius: " + radius;
}
/**返回速度*/
public int getSpeed() {
return speed;
}
/**设置速度*/
public void setSpeed(int speed) {
if(speed < SLOW)
this.speed = SLOW;
else if(speed > FAST)
this.speed = FAST;
else 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 > 0) ? radius : 0;
}
/**返回颜色*/
public String getColor() {
return color;
}
/**设置颜色*/
public void setColor(String color) {
this.color = color;
}
//————————————————————————————————————————————————————————————
public static void main(String[] args) {
Fan fan1 = new Fan();
fan1.setOn(true);
fan1.setSpeed(fan1.FAST);
fan1.setRadius(10);
fan1.setColor("yellow");
System.out.println("Fan1: \n" + fan1.toString());
Fan fan2 = new Fan();
fan2.setSpeed(fan2.MEDIUM);
System.out.println("\nFan2: \n" + fan2.toString());
}
}