Java写一个灯和空调风扇(能耗统计),基础数据:教室、灯、空调、风扇、电表
时间: 2023-07-04 19:11:16 浏览: 57
好的,以下是一个简单的Java实现,包括教室、灯、空调、风扇和电表类。
教室类 Classroom:
```java
public class Classroom {
private String name;
private Light light;
private AirConditioner airConditioner;
private Fan fan;
private ElectricityMeter electricityMeter;
public Classroom(String name) {
this.name = name;
light = new Light();
airConditioner = new AirConditioner();
fan = new Fan();
electricityMeter = new ElectricityMeter();
}
public void turnOnLight() {
light.turnOn();
electricityMeter.addElectricity(light.getPower());
}
public void turnOffLight() {
light.turnOff();
}
public void turnOnAirConditioner() {
airConditioner.turnOn();
electricityMeter.addElectricity(airConditioner.getPower());
}
public void turnOffAirConditioner() {
airConditioner.turnOff();
}
public void turnOnFan() {
fan.turnOn();
electricityMeter.addElectricity(fan.getPower());
}
public void turnOffFan() {
fan.turnOff();
}
public double getElectricityConsumption() {
return electricityMeter.getElectricityConsumption();
}
}
```
灯类 Light:
```java
public class Light {
private boolean powerOn;
private double power;
public Light() {
powerOn = false;
power = 10; // 单位:瓦
}
public void turnOn() {
powerOn = true;
System.out.println("灯已开");
}
public void turnOff() {
powerOn = false;
System.out.println("灯已关");
}
public boolean isPowerOn() {
return powerOn;
}
public double getPower() {
return power;
}
}
```
空调类 AirConditioner:
```java
public class AirConditioner {
private int temperature;
private int mode; // 0: 制冷模式,1: 制热模式
private boolean powerOn;
private double power;
public AirConditioner() {
temperature = 25;
mode = 0;
powerOn = false;
power = 1000; // 单位:瓦
}
public void turnOn() {
powerOn = true;
System.out.println("空调已开机");
}
public void turnOff() {
powerOn = false;
System.out.println("空调已关机");
}
public void setTemperature(int temperature) {
if (powerOn) {
if (mode == 0) { // 制冷模式
if (temperature >= 16 && temperature <= 30) {
this.temperature = temperature;
System.out.println("温度已调至" + temperature + "度");
} else {
System.out.println("制冷模式温度范围为16~30度");
}
} else { // 制热模式
if (temperature >= 16 && temperature <= 30) {
this.temperature = temperature;
System.out.println("温度已调至" + temperature + "度");
} else {
System.out.println("制热模式温度范围为16~30度");
}
}
} else {
System.out.println("请先开机");
}
}
public void setMode(int mode) {
if (powerOn) {
if (mode == 0 || mode == 1) {
this.mode = mode;
System.out.println(mode == 0 ? "已切换至制冷模式" : "已切换至制热模式");
} else {
System.out.println("模式参数错误,0表示制冷模式,1表示制热模式");
}
} else {
System.out.println("请先开机");
}
}
public int getTemperature() {
return temperature;
}
public int getMode() {
return mode;
}
public boolean isPowerOn() {
return powerOn;
}
public double getPower() {
return power;
}
}
```
风扇类 Fan:
```java
public class Fan {
private boolean powerOn;
private double power;
public Fan() {
powerOn = false;
power = 50; // 单位:瓦
}
public void turnOn() {
powerOn = true;
System.out.println("风扇已开");
}
public void turnOff() {
powerOn = false;
System.out.println("风扇已关");
}
public boolean isPowerOn() {
return powerOn;
}
public double getPower() {
return power;
}
}
```
电表类 ElectricityMeter:
```java
public class ElectricityMeter {
private double electricityConsumption;
public ElectricityMeter() {
electricityConsumption = 0;
}
public void addElectricity(double electricity) {
electricityConsumption += electricity;
}
public double getElectricityConsumption() {
return electricityConsumption;
}
}
```
以上是一个简单的实现,支持开关灯、空调和风扇,同时计算电表数据。可以根据具体需求进行扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)