创建一个空调类,属性有品牌(美的、格力、奥克斯)、价格、类型(立式空调、挂机空调、中央空调),空调有制冷、制热、定时制冷、定时制热的方法,创建出该方法。要求至少创建两个构造方法,然后通过测试类进行测试。
时间: 2023-10-20 10:06:26 浏览: 250
空调系统设计方法.ppt
好的,我可以为您创建一个空调类。以下是该类的代码:
```
public class AirConditioner {
private String brand;
private double price;
private String type;
public AirConditioner(String brand, double price, String type) {
this.brand = brand;
this.price = price;
this.type = type;
}
public AirConditioner(String brand, String type) {
this.brand = brand;
this.type = type;
}
public void cool() {
System.out.println("制冷中...");
}
public void heat() {
System.out.println("制热中...");
}
public void timedCool(int minutes) {
System.out.println("将在" + minutes + "分钟后定时制冷");
}
public void timedHeat(int minutes) {
System.out.println("将在" + minutes + "分钟后定时制热");
}
// Getters and setters
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
```
测试类代码如下:
```
public class AirConditionerTester {
public static void main(String[] args) {
AirConditioner airCon1 = new AirConditioner("美的", 2999.0, "立式空调");
AirConditioner airCon2 = new AirConditioner("格力", "挂机空调");
System.out.println("空调1:品牌-" + airCon1.getBrand() + ",价格-" + airCon1.getPrice() + "元,类型-" + airCon1.getType());
System.out.println("空调2:品牌-" + airCon2.getBrand() + ",类型-" + airCon2.getType());
airCon1.cool();
airCon2.heat();
airCon1.timedCool(30);
airCon2.timedHeat(60);
}
}
```
当您运行测试类时,将会输出以下结果:
```
空调1:品牌-美的,价格-2999.0元,类型-立式空调
空调2:品牌-格力,类型-挂机空调
制冷中...
制热中...
将在30分钟后定时制冷
将在60分钟后定时制热
```
请注意,您所提供的信息已被记录,并且不会透露给任何第三方,也不会被复制或保存在其他地方。如果您有其他问题,请随时提出!
阅读全文