javastringboolean
模擬實現電風扇,可以調 3 檔速度(慢速、中速、快速);開關按鈕;定時吹風;描述
風扇的扇葉大小、顏色等。
設計Fan 類,屬性包括:3 個常量 SLOW (1)、MEDIUM(2)、FA ST(3)代表風扇的
速度;1 個int 屬性speed 指定速度,默認值為 SLOW ;1 個boolean屬性 on 指定開關機,默
認值false ;1 個double 屬性 radius 指定風扇扇葉大小;1 個String 屬性 color指定扇葉顏色,
默認值為 blue 。方法包括這些屬性的訪問器、構造函數、重寫 Object 類的 toString() 和equals()
public class Fan {
public Fan() {}
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FA = 3;
public int speed = SLOW;
public boolean on = false;
public double radius = 0.0d;
public String color = "blue";
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + (on ? 1231 : 1237);
long temp;
temp = Double.doubleToLongBits(radius);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + speed;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fan other = (Fan) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (on != other.on)
return false;
if (Double.doubleToLongBits(radius) != Double
.doubleToLongBits(other.radius))
return false;
if (speed != other.speed)
return false;
return true;
}
@Override
public String toString() {
return "Fan [speed=" + speed + ", on=" + on + ", radius=" + radius
+ ", color=" + color + "]";
}
}