用Java语言建立一个家用电器类,作为实验3中建立的电视类的父类。(注意构造函数) 修改电视类的设计,使其继承于家用电器类。 增加一个空调类,使其同样继承于家用电器类。 设计空调类的成员属性和成员方法。(注意分析空调功能的多样性:制冷;制热;除湿;换新风等。) 同实验3相同,要考虑其构造函数的设计。 为本次实验设计一个测试类,用以实现以上类的功能设计。
时间: 2023-05-27 19:04:03 浏览: 81
家用电器类:
```java
public class Appliances {
private String brand;
private String model;
private double price;
public Appliances(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
电视类:
```java
public class Television extends Appliances {
private int size;
private String resolution;
private boolean smartTV;
public Television(String brand, String model, double price, int size, String resolution, boolean smartTV) {
super(brand, model, price);
this.size = size;
this.resolution = resolution;
this.smartTV = smartTV;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
public boolean isSmartTV() {
return smartTV;
}
public void setSmartTV(boolean smartTV) {
this.smartTV = smartTV;
}
}
```
空调类:
```java
public class AirConditioner extends Appliances {
private boolean heating;
private boolean cooling;
private boolean dehumidifying;
public AirConditioner(String brand, String model, double price, boolean heating, boolean cooling, boolean dehumidifying) {
super(brand, model, price);
this.heating = heating;
this.cooling = cooling;
this.dehumidifying = dehumidifying;
}
public boolean isHeating() {
return heating;
}
public void setHeating(boolean heating) {
this.heating = heating;
}
public boolean isCooling() {
return cooling;
}
public void setCooling(boolean cooling) {
this.cooling = cooling;
}
public boolean isDehumidifying() {
return dehumidifying;
}
public void setDehumidifying(boolean dehumidifying) {
this.dehumidifying = dehumidifying;
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
Television tv = new Television("Sony", "Bravia", 999.99, 50, "4K", true);
System.out.println(tv.getBrand());
System.out.println(tv.getModel());
System.out.println(tv.getPrice());
System.out.println(tv.getSize());
System.out.println(tv.getResolution());
System.out.println(tv.isSmartTV());
AirConditioner ac = new AirConditioner("Daikin", "Smart Inverter", 799.99, true, true, true);
System.out.println(ac.getBrand());
System.out.println(ac.getModel());
System.out.println(ac.getPrice());
System.out.println(ac.isHeating());
System.out.println(ac.isCooling());
System.out.println(ac.isDehumidifying());
}
}
```
阅读全文