You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.8 KiB
74 lines
1.8 KiB
/**
|
|
* 智能灯类
|
|
* 实现了Device和BrightnessAdjustable接口
|
|
*/
|
|
public class SmartLight implements Device, BrightnessAdjustable {
|
|
private String name;
|
|
private boolean on;
|
|
private int brightness;
|
|
|
|
public SmartLight(String name) {
|
|
this.name = name;
|
|
this.on = false;
|
|
this.brightness = 50; // 默认亮度50%
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
@Override
|
|
public void turnOn() {
|
|
if (!on) {
|
|
on = true;
|
|
System.out.println(name + " 智能灯已开启,当前亮度: " + brightness + "%");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void turnOff() {
|
|
if (on) {
|
|
on = false;
|
|
System.out.println(name + " 智能灯已关闭");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isOn() {
|
|
return on;
|
|
}
|
|
|
|
@Override
|
|
public void increaseBrightness(int level) {
|
|
if (on) {
|
|
brightness = Math.min(100, brightness + level);
|
|
System.out.println(name + " 亮度已增加到: " + brightness + "%");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void decreaseBrightness(int level) {
|
|
if (on) {
|
|
brightness = Math.max(0, brightness - level);
|
|
System.out.println(name + " 亮度已降低到: " + brightness + "%");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setBrightness(int brightness) {
|
|
if (on) {
|
|
if (brightness >= 0 && brightness <= 100) {
|
|
this.brightness = brightness;
|
|
System.out.println(name + " 亮度已设置为: " + brightness + "%");
|
|
} else {
|
|
System.out.println(name + " 亮度超出范围,请设置在 0-100 之间");
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getCurrentBrightness() {
|
|
return brightness;
|
|
}
|
|
} |